Search code examples
graphqlcode-generation

what's difference between schema and documents in Graphql?


what's the difference between schema and documents in Graphql?

schema is like this:

type Query {
   fo: String
}

but the document is like:

query SomeQuery {
  foo {
    bar
  }
}

the spec is really confusing https://graphql.github.io/graphql-spec/June2018/#sec-Language.Document

I always use schema but for client-side type generation in graphql-code-generator it needs document file. https://graphql-code-generator.com/docs/getting-started/documents-field


Solution

  • A document is really any string containing valid GraphQL syntax. According to the spec, a document contains one or more definitions, where a definition could be:

    an operation definition

    query UsersQuery {
      users {
        id
        email
      }
    }
    

    a fragment definition

    fragment UserFragment on User {
      id
      email
    }
    

    a type system definition

    type User {
      id: ID!
      email: String!
    }
    

    a type system extension

    extend type User {
      name: String
    }
    

    Operation and fragment definitions are known as executable definitions. Documents sent to a GraphQL service must only contain executable definitions. Type system definitions and extensions are used in describing a schema -- that's why we commonly call them Schema Definition Language (SDL). A schema is a GraphQL service's "collective type system capabilities" -- it's basically a collection of types and directives that represent everything your GraphQL service can do.

    A schema may be described using type system definitions, but it's not really accurate to say that the type definitions are the schema because the schema itself also includes the actual field resolution logic as well.