Hi everyone,
I’m using Apollo and GraphQL in an iOS project, and I’m trying to configure the code generation for my queries and mutations. Following the Apollo documentation, I noticed that it generates a Swift Package Manager (SPM) package by default. However, if I want to add multiple queries, this approach could potentially lead to unnecessary SPM packages, which I would prefer to avoid.
Currently, my configuration looks like this:
{
"schemaNamespace": "RocketReserverAPI",
"input": {
"operationSearchPaths": [
"**/*.graphql"
],
"schemaSearchPaths": [
"**/*.graphqls"
]
},
"output": {
"testMocks": {
"none": {}
},
"schemaTypes": {
"path": "./RocketReserverAPI",
"moduleType": {
"embeddedInTarget": {
"name": "GraphQLwithApolloSWAPI"
}
}
},
"operations": {
"inSchemaModule": {}
}
},
"schemaDownloadConfiguration": {
"downloadMethod": {
"introspection": {
"endpointURL": "https://swapi-graphql.netlify.app/.netlify/functions/index",
"httpMethod": {
"POST": {}
},
"includeDeprecatedInputValues": false,
"outputFormat": "SDL"
}
},
"downloadTimeout": 60,
"headers": [],
"outputPath": "./graphql/schema.graphqls"
}
}
When I run apollo-cli generate
, I encounter the following error:
Error: JavaScriptError: GraphQLError-Syntax Error: Expected Name, found String "__schema".-GraphQLError@
c@
expectToken@
parseName@
parseField@
many@
parseSelectionSet@
parseOperationDefinition@
many@
parseDocument@
Additionally, I came across external tutorials that suggest using a script like this:
# Derived Data Path where the source packages are checked out.
DERIVED_DATA_CANDIDATE="${BUILD_ROOT}"
while ! [ -d "${DERIVED_DATA_CANDIDATE}/SourcePackages" ]; do
if [ "${DERIVED_DATA_CANDIDATE}" = / ]; then
echo >&2 "error: Unable to locate SourcePackages directory from BUILD_ROOT: '${BUILD_ROOT}'"
exit 1
fi
DERIVED_DATA_CANDIDATE="$(dirname "${DERIVED_DATA_CANDIDATE}")"
done
# Grab a reference to the directory where scripts are checked out
SCRIPT_PATH="${DERIVED_DATA_CANDIDATE}/SourcePackages/checkouts/apollo-ios/scripts"
if [ -z "${SCRIPT_PATH}" ]; then
echo >&2 "error: Couldn't find the CLI script in your checked out SPM packages; make sure to add the framework to your project."
exit 1
fi
cd "${SRCROOT}/${TARGET_NAME}"
"${SCRIPT_PATH}"/run-bundled-codegen.sh codegen:generate --target=swift --includes=./**/*.graphql --localSchemaFile="schema.json" GraphQL/API.swift
However, in Apollo iOS version 1.15.3, the run-bundled-codegen.sh
script seems to be missing. It might have been removed or replaced.
What I’d really like to achieve is to automatically generate my models inside my project, not in an external package. Is this possible with the latest version of Apollo? If so, what’s the recommended approach?
Any guidance would be greatly appreciated. Thank you in advance!