I am serving this file on a local server:
schema.graphql
type Mutation {
register(email: String!, password: String!): Long!
login(email: String!, password: String!): TokenPair!
refreshToken(refreshToken: String!): TokenPair!
}
type Query {
getTask(id: Long!): Task
}
type Task {
taskId: Long!
name: String!
}
type TokenPair {
accessToken: String!
refreshToken: String!
}
and I use this command to download it in .json format.
apollo schema:download --endpoint=http://localhost:8080/schema.graphql schema.json
This is the response I get:
› Warning: apollo update available from 1.9.0 to 2.16.0.
✔ Loading Apollo config
✔ Fetching current schema
✖ Saving schema to schema.json
→ Expected undefined to be a GraphQL schema.
Error: Expected undefined to be a GraphQL schema.
at invariant (/usr/local/lib/node_modules/apollo/node_modules/graphql/jsutils/invariant.js:19:11)
at validateSchema (/usr/local/lib/node_modules/apollo/node_modules/graphql/type/validate.js:55:60)
at assertValidSchema (/usr/local/lib/node_modules/apollo/node_modules/graphql/type/validate.js:80:16)
at assertValidExecutionArguments (/usr/local/lib/node_modules/apollo/node_modules/graphql/execution/execute.js:201:35)
at executeImpl (/usr/local/lib/node_modules/apollo/node_modules/graphql/execution/execute.js:136:3)
at Object.execute (/usr/local/lib/node_modules/apollo/node_modules/graphql/execution/execute.js:131:229)
at Task.task (/usr/local/lib/node_modules/apollo/lib/commands/schema/download.js:31:113)
at Promise.resolve.then.then.skipped (/usr/local/lib/node_modules/apollo/node_modules/listr/lib/task.js:167:30)
at <anonymous>
Is there a way to use schema defined by SDL inside Apollo library or only JSON format is supported? Thanks
Can you try this with v0.13.0- looks like you're running 0.12 or lower there, and we've updated things to use the current version of the Apollo CLI, which I think will work better with this.
I tried to update Apollo pod but it still installed version 0.12. Is v0.13 published?
Yep, just published a couple hours ago - try running pod outdated - that forces a fetch of the up to date specs repo from trunk. If it's not showing up there, please let me know. I should have screenshotted the little 🚀bit 😃
I got the 0.13 pod now and also I updated apollo to 2.16.0.
I updated my Build script and get this error if I use schema file like in my first post above:
SCRIPT_PATH="${PODS_ROOT}/Apollo/scripts"
cd "${SRCROOT}/${TARGET_NAME}"
"${SCRIPT_PATH}"/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./*.graphql --localSchemaFile="schema.graphql" API.swift
++ npx --no-install apollo codegen:generate --target=swift '--includes=./*.graphql' --localSchemaFile=schema.graphql API.swift
Loading Apollo Project [started]
{ Error: Error in "Loading schema for Unnamed Project": Error: Unknown type "Long".
Unknown type "Long".
Unknown type "Long".
at Object.error (/usr/local/lib/node_modules/apollo/node_modules/@oclif/errors/lib/index.js:22:17)
at Generate.error (/usr/local/lib/node_modules/apollo/node_modules/@oclif/command/lib/command.js:57:23)
at OclifLoadingHandler.showError (/usr/local/lib/node_modules/apollo/lib/OclifLoadingHandler.js:28:22)
at OclifLoadingHandler.handle (/usr/local/lib/node_modules/apollo/lib/OclifLoadingHandler.js:13:18)
at <anonymous> oclif: { exit: 2 }, code: undefined }
Loading Apollo Project [failed]
→ Error initializing Apollo GraphQL project "Unnamed Project": Error: Error in "Loading schema for Unnamed Project": Error: Unknown type "Long".
Unknown type "Long".
Unknown type "Long".
› Error: Error initializing Apollo GraphQL project "Unnamed Project": Error:
› Error in "Loading schema for Unnamed Project": Error: Unknown type "Long".
›
› Unknown type "Long".
›
› Unknown type "Long".
Command PhaseScriptExecution failed with a nonzero exit code
Am I missing something here?
Long is a custom scalar for GraphQL - can you try using the --passthroughCustomScalars parameter?
I tried with adding that parameter, I get the same error:
SCRIPT_PATH="${PODS_ROOT}/Apollo/scripts"
cd "${SRCROOT}/${TARGET_NAME}"
"${SCRIPT_PATH}"/check-and-run-apollo-cli.sh codegen:generate --passthroughCustomScalars --target=swift --includes=./*.graphql --localSchemaFile="schema.graphql" API.swift
Checked in with a colleague and he recommended making sure that the schema includes a scalar Long to actually define Long as a custom scalar. Is that in there?
I tried with adding scalar Long. Here is the complete schema.gql:
scalar Long
type Mutation {
register(email: String!, password: String!): Long!
login(email: String!, password: String!): TokenPair!
refreshToken(refreshToken: String!): TokenPair!
}
type Query {
getTask(id: Long!): Task
}
type Task {
taskId: Long!
name: String!
}
type TokenPair {
accessToken: String!
refreshToken: String!
}
This is the error I am seeing now:
⚠️ It looks like there are 0 files associated with this Apollo Project. This may be because you don't have any files yet, or your includes/excludes fields are configured incorrectly, and Apollo can't find your files. For help configuring Apollo projects, see this guide: https://bit.ly/2ByILPj
Loading Apollo Project [started]
Loading Apollo Project [completed]
Generating query files [started]
Generating query files with 'swift' target [title changed]
Generating query files with 'swift' target [failed]
→ No operations or fragments found to generate code for.
Error: No operations or fragments found to generate code for.
at write (/usr/local/lib/node_modules/apollo/lib/commands/client/codegen.js:61:39)
at Task.task (/usr/local/lib/node_modules/apollo/lib/commands/client/codegen.js:86:46)
at <anonymous>
Command PhaseScriptExecution failed with a nonzero exit code
Do you have any .graphql files in your project? That seems to be what it's complaining about.
I do have one and it is empty. I tried with adding a custom scalar to it scalar SomeCustomScalar but I still get the same error (0 files associated...).
The script I am using:
SCRIPT_PATH="${PODS_ROOT}/Apollo/scripts"
cd "${SRCROOT}/${TARGET_NAME}"
"${SCRIPT_PATH}"/check-and-run-apollo-cli.sh codegen:generate --passthroughCustomScalars --target=swift --includes=./*.graphql --localSchemaFile="schema.gql" API.swift
Looks like it just does not sees the .graphql files.
If I rename schema.gql to schema.graphql and update script with --localSchemaFile="schema.graphql" I get different errors (pasted bellow). In what format is apollo script expecting schema file?
++ npx --no-install apollo codegen:generate --passthroughCustomScalars --target=swift '--includes=./*.graphql' --localSchemaFile=schema.graphql API.swift
Loading Apollo Project [started]
{ Error: Error in "Loading schema for Unnamed Project": Error: Type "Long" already exists in the schema. It cannot also be defined in this type definition.
Type "Mutation" already exists in the schema. It cannot also be defined in this type definition.
Field "Mutation.register" already exists in the schema. It cannot also be defined in this type extension.
Field "Mutation.login" already exists in the schema. It cannot also be defined in this type extension.
Field "Mutation.refreshToken" already exists in the schema. It cannot also be defined in this type extension.
Type "Query" already exists in the schema. It cannot also be defined in this type definition.
Field "Query.getTask" already exists in the schema. It cannot also be defined in this type extension.
Type "Task" already exists in the schema. It cannot also be defined in this type definition.
Field "Task.taskId" already exists in the schema. It cannot also be defined in this type extension.
Field "Task.name" already exists in the schema. It cannot also be defined in this type extension.
Type "TokenPair" already exists in the schema. It cannot also be defined in this type definition.
Field "TokenPair.accessToken" already exists in the schema. It cannot also be defined in this type extension.
Field "TokenPair.refreshToken" already exists in the schema. It cannot also be defined in this type extension.
at Object.error (/usr/local/lib/node_modules/apollo/node_modules/@oclif/errors/lib/index.js:22:17)
at Generate.error (/usr/local/lib/node_modules/apollo/node_modules/@oclif/command/lib/command.js:57:23)
at OclifLoadingHandler.showError (/usr/local/lib/node_modules/apollo/lib/OclifLoadingHandler.js:28:22)
at OclifLoadingHandler.handle (/usr/local/lib/node_modules/apollo/lib/OclifLoadingHandler.js:13:18)
at <anonymous> oclif: { exit: 2 }, code: undefined }
Loading Apollo Project [failed]
→ Error initializing Apollo GraphQL project "Unnamed Project": Error: Error in "Loading schema for Unnamed Project": Error: Type "Long" already exists in the schema. It cannot also be defined in this type definition.
Type "Mutation" already exists in the schema. It cannot also be defined in this type definition.
Field "Mutation.register" already exists in the schema. It cannot also be defined in this type extension.
Field "Mutation.login" already exists in the schema. It cannot also be defined in this type extension.
Field "Mutation.refreshToken" already exists in the schema. It cannot also be defined in this type extension.
Type "Query" already exists in the schema. It cannot also be defined in this type definition.
Field "Query.getTask" already exists in the schema. It cannot also be defined in this type extension.
Type "Task" already exists in the schema. It cannot also be defined in this type definition.
Field "Task.taskId" already exists in the schema. It cannot also be defined in this type extension.
Field "Task.name" already exists in the schema. It cannot also be defined in this type extension.
Type "TokenPair" already exists in the schema. It cannot also be defined in this type definition.
Field "TokenPair.accessToken" already exists in the schema. It cannot also be defined in this type extension.
Field "TokenPair.refreshToken" already exists in the schema. It cannot also be defined in this type extension.
› Error: Error initializing Apollo GraphQL project "Unnamed Project": Error:
› Error in "Loading schema for Unnamed Project": Error: Type "Long" already
› exists in the schema. It cannot also be defined in this type definition.
›
› Type "Mutation" already exists in the schema. It cannot also be defined in
› this type definition.
›
› Field "Mutation.register" already exists in the schema. It cannot also be
› defined in this type extension.
›
› Field "Mutation.login" already exists in the schema. It cannot also be
› defined in this type extension.
›
› Field "Mutation.refreshToken" already exists in the schema. It cannot also
› be defined in this type extension.
›
› Type "Query" already exists in the schema. It cannot also be defined in
› this type definition.
›
› Field "Query.getTask" already exists in the schema. It cannot also be
› defined in this type extension.
›
› Type "Task" already exists in the schema. It cannot also be defined in
› this type definition.
›
› Field "Task.taskId" already exists in the schema. It cannot also be
› defined in this type extension.
›
› Field "Task.name" already exists in the schema. It cannot also be defined
› in this type extension.
›
› Type "TokenPair" already exists in the schema. It cannot also be defined
› in this type definition.
›
› Field "TokenPair.accessToken" already exists in the schema. It cannot also
› be defined in this type extension.
›
› Field "TokenPair.refreshToken" already exists in the schema. It cannot
› also be defined in this type extension.
I get the same error by modifying the build phase. We have many .graphql files and it used to work before updating the pod and the build phase.
@victormihaita what is the format of your schema file?
.graphql is for the files which contain queries/mutations/fragments/subscriptions which need to be added, not for the schema. You should keep the schema as a .json file, but if it doesn't see any queries to use to generate code, you'll get the No operations or fragments found to generate code for error.
@victormihaita can you paste your build phase in here? I'm thinking there might be some stray quotes or somesuch going on.
QUOTED_FRAMEWORK_SEARCH_PATHS=\"$(echo $FRAMEWORK_SEARCH_PATHS | tr -d '"' | sed -e 's/ \//" "\//g')\"
APOLLO_FRAMEWORK_PATH="$(eval find ${QUOTED_FRAMEWORK_SEARCH_PATHS} -name "Apollo.framework" -maxdepth 1 -print | head -n 1)"
if [ -z "${APOLLO_FRAMEWORK_PATH}" ]; then
echo "error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project."
exit 1
fi
cd "${SRCROOT}/${TARGET_NAME}"
"${APOLLO_FRAMEWORK_PATH}"/check-and-run-apollo-cli.sh codegen:generate --target=swift --includes=./*.graphql --localSchemaFile="Sources/API/Generated/schema.json" --passthroughCustomScalars Sources/API/Generated/API.swift
Hm. Yeah that looks correct - @victormihaita I'm going to open a separate issue so you and I can hunt your issue down and @Dino4674 and I can continue to hunt his down
@Dino4674 I think I missed something critical here earlier - I'm not actually positive we're able to parse .gql schema files completely correctly. Can you see if getting a .json file works better for you?
@designatednerd We are following the docs line by line. It instructs us to add the .graphql files as the last step, but this new version assumes you already have .graphql files during the build steps, which sounds like a communication miss. We are using a .json file format and am running into the same issue as @Dino4674.
Let us know if you need any information from us to help track down @Dino4674's issue. Our schema.json file was autogenerated through the cli.
@cmcaboy Can you link me to what you're having trouble with - I thought I'd changed that order but it's very possible I missed someplace where the instructions are different.
@designatednerd @cmcaboy and I are working together. Trying to get a basic iOS project running. We're following the installation guide to the t. I downloded the schema.json, added it to my target, copied the build script and tried to build my project but I'm getting the same error as this
Yep, that's because there are no GraphQL queries in your codebase at the moment - I'm updating the docs as we speak
If you go through to the "Creating .graphql files with your queries or mutations" section, that should get that issue resolved. Basically, once there's a .graphql file with a valid query in there, it should stop that whining
@designatednerd Awesome! Thank you so much.
@cmcaboy @akhilpothana Take a look at #662, let me know if you're still having issues after that order swap
@designatednerd We did get passed that error, but we ran into another error. I'm not sure if it is related. We may be missing something.
Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.
(node:58421) UnhandledPromiseRejectionWarning: Error: Cannot use GraphQLScalarType "String" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.
Yep, check out the release notes for 0.13.0 - just nuke your local node_modules that's within your source root and that'll check everything back out.
I'll add that to the general docs.
@designatednerd That worked! Thank you!
@designatednerd I tried with different schema.json (link bellow) and code generated successfully with newest Apollo CLI and Apollo pod. I also had to add at least one .graphql file for build to succeed and it was the first time I saw something inside API.swift instead of the empty file with single import Apollo header.
We will need to convert our schema to json since Apollo iOS library is having difficulties with SDL format. Is it official that SDL is not supported or there are just bugs in the feature?
@Dino4674 I think I missed something critical here earlier - I'm not actually positive we're able to parse
.gqlschema files completely correctly. Can you see if getting a.jsonfile works better for you?
@Dino4674 I've updated the instructions for setup in #662 to make sure you add a .graphql file before setting up the build step, and to have better info about how to troubleshoot if you get some of the errors you were getting.
I would say your question about SDL format support is probably a better one for the apollo-tooling repo - that's the bit of code that actually does all the analysis of any file passed in through --localSchemaFile.
I'm hoping to get some better separation of concerns going between that and the actual swift code generation soon, but I don't have a timeline for that, unfortunately.
@Dino4674 I'm gonna close this out since I've merged the updated instructions in #662 and your remaining question is one that needs to be addressed to the apollo-tooling repo - please feel free to open another issue if you run into more problems!
Most helpful comment
Yep, that's because there are no GraphQL queries in your codebase at the moment - I'm updating the docs as we speak