v0.16.2 (or earlier) doesn't properly generate the JSON when "extend type" is used. Only the definition without the extended properties is written out to the json file. We use "extend type" for apollo-server to allow the types to exist across different files but not throw IDE errors.
An example case:
type Query {
version:String
}
type Mutation {
}
extend type Query {
#get a user
userExists(id:ID) : Boolean
}
schema {
query: Query
mutation: Mutation
}
Apollo-codegen will not provide the "userExists" definition in the Query type it generates to JSON. Only the "version" property will be scripted.
Related to this it should be possible to run an introspection like:
apollo-codegen introspect-schema ./*/.graphqls --output ./schema.json
With "extend type" in some of the files and the actual type specified in another file without getting the following error "Must provide schema definition with query type or a type named Query."
i.e. One manner to handle that may be to use loadAndMergeQueryDocuments similar to the "apollo-codegen generate" CLI command rather than assuming that the full document is in a single file.
@matthewerwin You should open a second issue to track the related issue you saw. I think there is some discussion necessary on that second one. (the main issue you reported here is definitely a bug we want to fix)
@rricard per my comments on #211 I have a fix already in place for this. Will see if I can get everything pulled together for review by your team quickly after testing. I can issue a PR separately for the bug since the other additions involve a lot more code.
For my sub-comment above it was just a matter of switching to "glob" when isMagic()=true and using the loadAndMergeQueryDocuments( ) to splice everything together just like generate does with the inputPaths. I then called extendSchema( ) from graphql library the same way it's done by their team to get the proper AST and works for both introspect-schema and generate.
This should definitely be supported, since extend is part of the SDL spec.
This would be also very useful for type extensions on the client using apollo-link-state.
Currently it seems to be impossible to use apollo-codegen for Queries that use types like this:
// TypeDef on the Client
// (Person is a Type that's also contained in the Server's schema)
const typeDefs = `
extend type Person {
fullName: String!
}
`
(Note than on the client schema you just can leave out the extend but that's semantically not 100% correct I think)
Curious, why was this issue closed? This issue still seems to be present.
As a workaround, I remove the extend keyword prior to merging the schema using merge-graphql-schemas I then use only the merged schema one…
import { fileLoader, mergeTypes } from 'merge-graphql-schemas';
import { writeFileSync } from 'fs';
const loadedFiles = fileLoader(`${process.cwd()}/src/**/*.graphql`);
const withoutExtendFiles = loadedFiles.map((schema) => schema.replace(/extend *type/g, 'type'));
const typeDefs = mergeTypes(withoutExtendFiles, { all: true });
writeFileSync('./generated/merged.schema.graphql', typeDefs);
FWIW, we did not have the same issue working with https://github.com/dotansimha/graphql-code-generator, which we've starting using recently partly due to this issue.
Most helpful comment
Curious, why was this issue closed? This issue still seems to be present.