vadistic / graphql-extra

GraphQL AST/SDL toolkit extending graphql/graphql-js with extra features for code generation & testing
https://graphql-extra.netlify.com/globals
MIT License
15 stars 3 forks source link

Schema root operations break DocumentAPI #2

Closed GavinRay97 closed 4 years ago

GavinRay97 commented 4 years ago

Through a lucky guess, I found that removing the schema root operations will allow you to parse a schema with documentApi.addSDL():

const schema = buildClientSchema(content.introspection_schema)
const schemaString = printSchema(schema)
const removeSchemaRoot = (schemaString: string) => schemaString.replace(`
schema {
  query: query_root
  mutation: mutation_root
  subscription: subscription_root
}`, '')
const schemaWithoutRoot = removeSchemaRoot(schemaString)
console.log(schemaString)
console.log(removeSchemaRoot(schemaString))
// Magic: cut the top-level schema root with query/mutation/subscription off
const document = documentApi().addSDL(schemaWithoutRoot)

It also works if you trim roughly the first ~90 chars off with substr (but that's a terrible pattern):

const schemaWithoutRoot = schemaString.substr(90)
vadistic commented 4 years ago

Yeah, that was serious overlook, but it's fixed on master :)

https://github.com/vadistic/graphql-extra/blob/d2367aedf3a9822bb24c8c88cfacf082b7d89587/src/core/document/document-schema-base.ts#L223-L239

Currently there is set of getter methods on documentSchemaApi https://github.com/vadistic/graphql-extra/blob/d2367aedf3a9822bb24c8c88cfacf082b7d89587/src/core/document/document-schema.ts#L23-L50

I think there should be something like SchemaDefinitionApi for this for full schema root crud :)

vadistic commented 4 years ago

Yeah, so the above was totally rewritten - but now there is support for every kind of ast node.

https://github.com/vadistic/graphql-extra/blob/2b22d7334f0457c60b27fd18ce2e3b949054f9fe/src/kind-to-api.ts#L7-L79