what's difference between schema and documents in Graphql?
Asked Answered
W

2

8

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

Weitzel answered 19/12, 2019 at 21:38 Comment(0)
P
10

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.

Plan answered 19/12, 2019 at 22:55 Comment(0)
W
0

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).

Windblown answered 20/4 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.