{"id":13433154,"url":"https://github.com/graphql/graphql-spec","last_synced_at":"2025-05-14T08:05:14.399Z","repository":{"id":34411735,"uuid":"38342221","full_name":"graphql/graphql-spec","owner":"graphql","description":"GraphQL is a query language and execution engine tied to any backend service.","archived":false,"fork":false,"pushed_at":"2025-05-01T18:07:05.000Z","size":4817,"stargazers_count":14371,"open_issues_count":217,"forks_count":1139,"subscribers_count":512,"default_branch":"main","last_synced_at":"2025-05-07T07:14:09.309Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://spec.graphql.org","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/graphql.png","metadata":{"files":{"readme":"README.md","changelog":"changelogs/October2021.md","contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-07-01T01:26:56.000Z","updated_at":"2025-05-07T00:45:38.000Z","dependencies_parsed_at":"2023-02-13T04:30:38.223Z","dependency_job_id":"54ce1497-c0eb-46dd-81a5-8105ff1e692b","html_url":"https://github.com/graphql/graphql-spec","commit_stats":{"total_commits":490,"total_committers":139,"mean_commits":"3.5251798561151078","dds":0.6306122448979592,"last_synced_commit":"e546aacd22b6a434546fbc400540b5bd5cda0f64"},"previous_names":["facebook/graphql"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql%2Fgraphql-spec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql%2Fgraphql-spec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql%2Fgraphql-spec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphql%2Fgraphql-spec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphql","download_url":"https://codeload.github.com/graphql/graphql-spec/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101588,"owners_count":22014907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T02:01:21.746Z","updated_at":"2025-05-14T08:05:14.374Z","avatar_url":"https://github.com/graphql.png","language":"Shell","readme":"[![GraphQLConf 2025 Banner: September 08-10, Amsterdam. Hosted by the GraphQL Foundation](./assets/graphql.org_conf_2025_.png)](https://graphql.org/conf/2025/?utm_source=github\u0026utm_medium=graphql_js\u0026utm_campaign=readme)\n\n# GraphQL\n\n\u003cimg alt=\"GraphQL Logo\" align=\"right\" src=\"https://graphql.org/img/logo.svg\" width=\"15%\" /\u003e\n\nThe GraphQL specification is edited in the markdown files found in\n[`/spec`](./spec) the latest release of which is published at\nhttps://graphql.github.io/graphql-spec/.\n\nThe latest draft specification can be found at\nhttps://graphql.github.io/graphql-spec/draft/ which tracks the latest commit to\nthe main branch in this repository.\n\nPrevious releases of the GraphQL specification can be found at permalinks that\nmatch their [release tag](https://github.com/graphql/graphql-spec/releases). For\nexample, https://graphql.github.io/graphql-spec/October2016/. If you are linking\ndirectly to the GraphQL specification, it's best to link to a tagged permalink\nfor the particular referenced version.\n\n## Overview\n\nThis is a Working Draft of the Specification for GraphQL, a query language for\nAPIs created by Facebook.\n\nThe target audience for this specification is not the client developer, but\nthose who have, or are actively interested in, building their own GraphQL\nimplementations and tools.\n\nIn order to be broadly adopted, GraphQL will have to target a wide variety of\nbackend environments, frameworks, and languages, which will necessitate a\ncollaborative effort across projects and organizations. This specification\nserves as a point of coordination for this effort.\n\nLooking for help? Find resources\n[from the community](https://graphql.org/community/).\n\n## Getting Started\n\nGraphQL consists of a type system, query language and execution semantics,\nstatic validation, and type introspection, each outlined below. To guide you\nthrough each of these components, we've written an example designed to\nillustrate the various pieces of GraphQL.\n\nThis example is not comprehensive, but it is designed to quickly introduce the\ncore concepts of GraphQL, to provide some context before diving into the more\ndetailed specification or the\n[GraphQL.js](https://github.com/graphql/graphql-js) reference implementation.\n\nThe premise of the example is that we want to use GraphQL to query for\ninformation about characters and locations in the original Star Wars trilogy.\n\n### Type System\n\nAt the heart of any GraphQL implementation is a description of what types of\nobjects it can return, described in a GraphQL type system and returned in the\nGraphQL Schema.\n\nFor our Star Wars example, the\n[starWarsSchema.ts](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsSchema.ts)\nfile in GraphQL.js defines this type system.\n\nThe most basic type in the system will be `Human`, representing characters like\nLuke, Leia, and Han. All humans in our type system will have a name, so we\ndefine the `Human` type to have a field called \"name\". This returns a String,\nand we know that it is not null (since all `Human`s have a name), so we will\ndefine the \"name\" field to be a non-nullable String. Using a shorthand notation\nthat we will use throughout the spec and documentation, we would describe the\nhuman type as:\n\n```graphql\ntype Human {\n  name: String\n}\n```\n\nThis shorthand is convenient for describing the basic shape of a type system;\nthe JavaScript implementation is more full-featured, and allows types and fields\nto be documented. It also sets up the mapping between the type system and the\nunderlying data; for a test case in GraphQL.js, the underlying data is a\n[set of JavaScript objects](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsData.ts),\nbut in most cases the backing data will be accessed through some service, and\nthis type system layer will be responsible for mapping from types and fields to\nthat service.\n\nA common pattern in many APIs, and indeed in GraphQL is to give objects an ID\nthat can be used to refetch the object. So let's add that to our Human type.\nWe'll also add a string for their home planet.\n\n```graphql\ntype Human {\n  id: String\n  name: String\n  homePlanet: String\n}\n```\n\nSince we're talking about the Star Wars trilogy, it would be useful to describe\nthe episodes in which each character appears. To do so, we'll first define an\nenum, which lists the three episodes in the trilogy:\n\n```graphql\nenum Episode {\n  NEWHOPE\n  EMPIRE\n  JEDI\n}\n```\n\nNow we want to add a field to `Human` describing what episodes they were in.\nThis will return a list of `Episode`s:\n\n```graphql\ntype Human {\n  id: String\n  name: String\n  appearsIn: [Episode]\n  homePlanet: String\n}\n```\n\nNow, let's introduce another type, `Droid`:\n\n```graphql\ntype Droid {\n  id: String\n  name: String\n  appearsIn: [Episode]\n  primaryFunction: String\n}\n```\n\nNow we have two types! Let's add a way of going between them: humans and droids\nboth have friends. But humans can be friends with both humans and droids. How do\nwe refer to either a human or a droid?\n\nIf we look, we note that there's common functionality between humans and droids;\nthey both have IDs, names, and episodes in which they appear. So we'll add an\ninterface, `Character`, and make both `Human` and `Droid` implement it. Once we\nhave that, we can add the `friends` field, that returns a list of `Character`s.\n\nOur type system so far is:\n\n```graphql\nenum Episode {\n  NEWHOPE\n  EMPIRE\n  JEDI\n}\n\ninterface Character {\n  id: String\n  name: String\n  friends: [Character]\n  appearsIn: [Episode]\n}\n\ntype Human implements Character {\n  id: String\n  name: String\n  friends: [Character]\n  appearsIn: [Episode]\n  homePlanet: String\n}\n\ntype Droid implements Character {\n  id: String\n  name: String\n  friends: [Character]\n  appearsIn: [Episode]\n  primaryFunction: String\n}\n```\n\nOne question we might ask, though, is whether any of those fields can return\n`null`. By default, `null` is a permitted value for any type in GraphQL, since\nfetching data to fulfill a GraphQL query often requires talking to different\nservices that may or may not be available. However, if the type system can\nguarantee that a type is never null, then we can mark it as Non Null in the type\nsystem. We indicate that in our shorthand by adding an \"!\" after the type. We\ncan update our type system to note that the `id` is never null.\n\nNote that while in our current implementation, we can guarantee that more fields\nare non-null (since our current implementation has hard-coded data), we didn't\nmark them as non-null. One can imagine we would eventually replace our hardcoded\ndata with a backend service, which might not be perfectly reliable; by leaving\nthese fields as nullable, we allow ourselves the flexibility to eventually\nreturn null to indicate a backend error, while also telling the client that the\nerror occurred.\n\n```graphql\nenum Episode {\n  NEWHOPE\n  EMPIRE\n  JEDI\n}\n\ninterface Character {\n  id: String!\n  name: String\n  friends: [Character]\n  appearsIn: [Episode]\n}\n\ntype Human implements Character {\n  id: String!\n  name: String\n  friends: [Character]\n  appearsIn: [Episode]\n  homePlanet: String\n}\n\ntype Droid implements Character {\n  id: String!\n  name: String\n  friends: [Character]\n  appearsIn: [Episode]\n  primaryFunction: String\n}\n```\n\nWe're missing one last piece: an entry point into the type system.\n\nWhen we define a schema, we define an object type that is the basis for all\nquery operations. The name of this type is `Query` by convention, and it\ndescribes our public, top-level API. Our `Query` type for this example will look\nlike this:\n\n```graphql\ntype Query {\n  hero(episode: Episode): Character\n  human(id: String!): Human\n  droid(id: String!): Droid\n}\n```\n\nIn this example, there are three top-level operations that can be done on our\nschema:\n\n- `hero` returns the `Character` who is the hero of the Star Wars trilogy; it\n  takes an optional argument that allows us to fetch the hero of a specific\n  episode instead.\n- `human` accepts a non-null string as a query argument, a human's ID, and\n  returns the human with that ID.\n- `droid` does the same for droids.\n\nThese fields demonstrate another feature of the type system, the ability for a\nfield to specify arguments that configure their behavior.\n\nWhen we package the whole type system together, defining the `Query` type above\nas our entry point for queries, this creates a GraphQL Schema.\n\nThis example just scratched the surface of the type system. The specification\ngoes into more detail about this topic in the \"Type System\" section, and the\n[type](https://github.com/graphql/graphql-js/blob/main/src/type) directory in\nGraphQL.js contains code implementing a specification-compliant GraphQL type\nsystem.\n\n### Query Syntax\n\nGraphQL queries declaratively describe what data the issuer wishes to fetch from\nwhoever is fulfilling the GraphQL query.\n\nFor our Star Wars example, the\n[starWarsQueryTests.js](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsQuery-test.ts)\nfile in the GraphQL.js repository contains a number of queries and responses.\nThat file is a test file that uses the schema discussed above and a set of\nsample data, located in\n[starWarsData.js](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsData.ts).\nThis test file can be run to exercise the reference implementation.\n\nAn example query on the above schema would be:\n\n```graphql\nquery HeroNameQuery {\n  hero {\n    name\n  }\n}\n```\n\nThe initial line, `query HeroNameQuery`, defines a query with the operation name\n`HeroNameQuery` that starts with the schema's root query type; in this case,\n`Query`. As defined above, `Query` has a `hero` field that returns a\n`Character`, so we'll query for that. `Character` then has a `name` field that\nreturns a `String`, so we query for that, completing our query. The result of\nthis query would then be:\n\n```json\n{\n  \"hero\": {\n    \"name\": \"R2-D2\"\n  }\n}\n```\n\nSpecifying the `query` keyword and an operation name is only required when a\nGraphQL document defines multiple operations. We therefore could have written\nthe previous query with the query shorthand:\n\n```graphql\n{\n  hero {\n    name\n  }\n}\n```\n\nAssuming that the backing data for the GraphQL server identified R2-D2 as the\nhero. The response continues to vary based on the request; if we asked for\nR2-D2's ID and friends with this query:\n\n```graphql\nquery HeroNameAndFriendsQuery {\n  hero {\n    id\n    name\n    friends {\n      id\n      name\n    }\n  }\n}\n```\n\nthen we'll get back a response like this:\n\n```json\n{\n  \"hero\": {\n    \"id\": \"2001\",\n    \"name\": \"R2-D2\",\n    \"friends\": [\n      {\n        \"id\": \"1000\",\n        \"name\": \"Luke Skywalker\"\n      },\n      {\n        \"id\": \"1002\",\n        \"name\": \"Han Solo\"\n      },\n      {\n        \"id\": \"1003\",\n        \"name\": \"Leia Organa\"\n      }\n    ]\n  }\n}\n```\n\nOne of the key aspects of GraphQL is its ability to nest queries. In the above\nquery, we asked for R2-D2's friends, but we can ask for more information about\neach of those objects. So let's construct a query that asks for R2-D2's friends,\ngets their name and episode appearances, then asks for each of _their_ friends.\n\n```graphql\nquery NestedQuery {\n  hero {\n    name\n    friends {\n      name\n      appearsIn\n      friends {\n        name\n      }\n    }\n  }\n}\n```\n\nwhich will give us the nested response\n\n```json\n{\n  \"hero\": {\n    \"name\": \"R2-D2\",\n    \"friends\": [\n      {\n        \"name\": \"Luke Skywalker\",\n        \"appearsIn\": [\"NEWHOPE\", \"EMPIRE\", \"JEDI\"],\n        \"friends\": [\n          { \"name\": \"Han Solo\" },\n          { \"name\": \"Leia Organa\" },\n          { \"name\": \"C-3PO\" },\n          { \"name\": \"R2-D2\" }\n        ]\n      },\n      {\n        \"name\": \"Han Solo\",\n        \"appearsIn\": [\"NEWHOPE\", \"EMPIRE\", \"JEDI\"],\n        \"friends\": [\n          { \"name\": \"Luke Skywalker\" },\n          { \"name\": \"Leia Organa\" },\n          { \"name\": \"R2-D2\" }\n        ]\n      },\n      {\n        \"name\": \"Leia Organa\",\n        \"appearsIn\": [\"NEWHOPE\", \"EMPIRE\", \"JEDI\"],\n        \"friends\": [\n          { \"name\": \"Luke Skywalker\" },\n          { \"name\": \"Han Solo\" },\n          { \"name\": \"C-3PO\" },\n          { \"name\": \"R2-D2\" }\n        ]\n      }\n    ]\n  }\n}\n```\n\nThe `Query` type above defined a way to fetch a human given their ID. We can use\nit by hard-coding the ID in the query:\n\n```graphql\nquery FetchLukeQuery {\n  human(id: \"1000\") {\n    name\n  }\n}\n```\n\nto get\n\n```json\n{\n  \"human\": {\n    \"name\": \"Luke Skywalker\"\n  }\n}\n```\n\nAlternately, we could have defined the query to have a query parameter:\n\n```graphql\nquery FetchSomeIDQuery($someId: String!) {\n  human(id: $someId) {\n    name\n  }\n}\n```\n\nThis query is now parameterized by `$someId`; to run it, we must provide that\nID. If we ran it with `$someId` set to \"1000\", we would get Luke; set to \"1002\",\nwe would get Han. If we passed an invalid ID here, we would get `null` back for\nthe `human`, indicating that no such object exists.\n\nNotice that the key in the response is the name of the field, by default. It is\nsometimes useful to change this key, for clarity or to avoid key collisions when\nfetching the same field with different arguments.\n\nWe can do that with field aliases, as demonstrated in this query:\n\n```graphql\nquery FetchLukeAliased {\n  luke: human(id: \"1000\") {\n    name\n  }\n}\n```\n\nWe aliased the result of the `human` field to the key `luke`. Now the response\nis:\n\n```json\n{\n  \"luke\": {\n    \"name\": \"Luke Skywalker\"\n  }\n}\n```\n\nNotice the key is \"luke\" and not \"human\", as it was in our previous example\nwhere we did not use the alias.\n\nThis is particularly useful if we want to use the same field twice with\ndifferent arguments, as in the following query:\n\n```graphql\nquery FetchLukeAndLeiaAliased {\n  luke: human(id: \"1000\") {\n    name\n  }\n  leia: human(id: \"1003\") {\n    name\n  }\n}\n```\n\nWe aliased the result of the first `human` field to the key `luke`, and the\nsecond to `leia`. So the result will be:\n\n```json\n{\n  \"luke\": {\n    \"name\": \"Luke Skywalker\"\n  },\n  \"leia\": {\n    \"name\": \"Leia Organa\"\n  }\n}\n```\n\nNow imagine we wanted to ask for Luke and Leia's home planets. We could do so\nwith this query:\n\n```graphql\nquery DuplicateFields {\n  luke: human(id: \"1000\") {\n    name\n    homePlanet\n  }\n  leia: human(id: \"1003\") {\n    name\n    homePlanet\n  }\n}\n```\n\nbut we can already see that this could get unwieldy, since we have to add new\nfields to both parts of the query. Instead, we can extract out the common fields\ninto a fragment, and include the fragment in the query, like this:\n\n```graphql\nquery UseFragment {\n  luke: human(id: \"1000\") {\n    ...HumanFragment\n  }\n  leia: human(id: \"1003\") {\n    ...HumanFragment\n  }\n}\n\nfragment HumanFragment on Human {\n  name\n  homePlanet\n}\n```\n\nBoth of those queries give this result:\n\n```json\n{\n  \"luke\": {\n    \"name\": \"Luke Skywalker\",\n    \"homePlanet\": \"Tatooine\"\n  },\n  \"leia\": {\n    \"name\": \"Leia Organa\",\n    \"homePlanet\": \"Alderaan\"\n  }\n}\n```\n\nThe `UseFragment` and `DuplicateFields` queries will both get the same result,\nbut `UseFragment` is less verbose; if we wanted to add more fields, we could add\nit to the common fragment rather than copying it into multiple places.\n\nWe defined the type system above, so we know the type of each object in the\noutput; the query can ask for that type using the special field `__typename`,\ndefined on every object.\n\n```graphql\nquery CheckTypeOfR2 {\n  hero {\n    __typename\n    name\n  }\n}\n```\n\nSince R2-D2 is a droid, this will return\n\n```json\n{\n  \"hero\": {\n    \"__typename\": \"Droid\",\n    \"name\": \"R2-D2\"\n  }\n}\n```\n\nThis was particularly useful because `hero` was defined to return a `Character`,\nwhich is an interface; we might want to know what concrete type was actually\nreturned. If we instead asked for the hero of Episode V:\n\n```graphql\nquery CheckTypeOfLuke {\n  hero(episode: EMPIRE) {\n    __typename\n    name\n  }\n}\n```\n\nWe would find that it was Luke, who is a Human:\n\n```json\n{\n  \"hero\": {\n    \"__typename\": \"Human\",\n    \"name\": \"Luke Skywalker\"\n  }\n}\n```\n\nAs with the type system, this example just scratched the surface of the query\nlanguage. The specification goes into more detail about this topic in the\n\"Language\" section, and the\n[language](https://github.com/graphql/graphql-js/blob/main/src/language)\ndirectory in GraphQL.js contains code implementing a specification-compliant\nGraphQL query language parser and lexer.\n\n### Validation\n\nBy using the type system, it can be predetermined whether a GraphQL query is\nvalid or not. This allows servers and clients to effectively inform developers\nwhen an invalid query has been created, without having to rely on runtime\nchecks.\n\nFor our Star Wars example, the file\n[starWarsValidationTests.js](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsValidation-test.ts)\ncontains a number of demonstrations of invalid operations, and is a test file\nthat can be run to exercise the reference implementation's validator.\n\nTo start, let's take a complex valid query. This is the `NestedQuery` example\nfrom the above section, but with the duplicated fields factored out into a\nfragment:\n\n```graphql\nquery NestedQueryWithFragment {\n  hero {\n    ...NameAndAppearances\n    friends {\n      ...NameAndAppearances\n      friends {\n        ...NameAndAppearances\n      }\n    }\n  }\n}\n\nfragment NameAndAppearances on Character {\n  name\n  appearsIn\n}\n```\n\nAnd this query is valid. Let's take a look at some invalid queries!\n\nWhen we query for fields, we have to query for a field that exists on the given\ntype. So as `hero` returns a `Character`, we have to query for a field on\n`Character`. That type does not have a `favoriteSpaceship` field, so this query:\n\n```graphql\n# INVALID: favoriteSpaceship does not exist on Character\nquery HeroSpaceshipQuery {\n  hero {\n    favoriteSpaceship\n  }\n}\n```\n\nis invalid.\n\nWhenever we query for a field and it returns something other than a scalar or an\nenum, we need to specify what data we want to get back from the field. Hero\nreturns a `Character`, and we've been requesting fields like `name` and\n`appearsIn` on it; if we omit that, the query will not be valid:\n\n```graphql\n# INVALID: hero is not a scalar, so fields are needed\nquery HeroNoFieldsQuery {\n  hero\n}\n```\n\nSimilarly, if a field is a scalar, it doesn't make sense to query for additional\nfields on it, and doing so will make the query invalid:\n\n```graphql\n# INVALID: name is a scalar, so fields are not permitted\nquery HeroFieldsOnScalarQuery {\n  hero {\n    name {\n      firstCharacterOfName\n    }\n  }\n}\n```\n\nEarlier, it was noted that a query can only query for fields on the type in\nquestion; when we query for `hero` which returns a `Character`, we can only\nquery for fields that exist on `Character`. What happens if we want to query for\nR2-D2s primary function, though?\n\n```graphql\n# INVALID: primaryFunction does not exist on Character\nquery DroidFieldOnCharacter {\n  hero {\n    name\n    primaryFunction\n  }\n}\n```\n\nThat query is invalid, because `primaryFunction` is not a field on `Character`.\nWe want some way of indicating that we wish to fetch `primaryFunction` if the\n`Character` is a `Droid`, and to ignore that field otherwise. We can use the\nfragments we introduced earlier to do this. By setting up a fragment defined on\n`Droid` and including it, we ensure that we only query for `primaryFunction`\nwhere it is defined.\n\n```graphql\nquery DroidFieldInFragment {\n  hero {\n    name\n    ...DroidFields\n  }\n}\n\nfragment DroidFields on Droid {\n  primaryFunction\n}\n```\n\nThis query is valid, but it's a bit verbose; named fragments were valuable above\nwhen we used them multiple times, but we're only using this one once. Instead of\nusing a named fragment, we can use an inline fragment; this still allows us to\nindicate the type we are querying on, but without naming a separate fragment:\n\n```graphql\nquery DroidFieldInInlineFragment {\n  hero {\n    name\n    ... on Droid {\n      primaryFunction\n    }\n  }\n}\n```\n\nThis has just scratched the surface of the validation system; there are a number\nof validation rules in place to ensure that a GraphQL query is semantically\nmeaningful. The specification goes into more detail about this topic in the\n\"Validation\" section, and the\n[validation](https://github.com/graphql/graphql-js/blob/main/src/validation)\ndirectory in GraphQL.js contains code implementing a specification-compliant\nGraphQL validator.\n\n### Introspection\n\nIt's often useful to ask a GraphQL schema for information about what queries it\nsupports. GraphQL allows us to do so using the introspection system!\n\nFor our Star Wars example, the file\n[starWarsIntrospectionTests.js](https://github.com/graphql/graphql-js/blob/main/src/__tests__/starWarsIntrospection-test.ts)\ncontains a number of queries demonstrating the introspection system, and is a\ntest file that can be run to exercise the reference implementation's\nintrospection system.\n\nWe designed the type system, so we know what types are available, but if we\ndidn't, we can ask GraphQL, by querying the `__schema` field, always available\non the root type of a Query. Let's do so now, and ask what types are available.\n\n```graphql\nquery IntrospectionTypeQuery {\n  __schema {\n    types {\n      name\n    }\n  }\n}\n```\n\nand we get back:\n\n```json\n{\n  \"__schema\": {\n    \"types\": [\n      {\n        \"name\": \"Query\"\n      },\n      {\n        \"name\": \"Character\"\n      },\n      {\n        \"name\": \"Human\"\n      },\n      {\n        \"name\": \"String\"\n      },\n      {\n        \"name\": \"Episode\"\n      },\n      {\n        \"name\": \"Droid\"\n      },\n      {\n        \"name\": \"__Schema\"\n      },\n      {\n        \"name\": \"__Type\"\n      },\n      {\n        \"name\": \"__TypeKind\"\n      },\n      {\n        \"name\": \"Boolean\"\n      },\n      {\n        \"name\": \"__Field\"\n      },\n      {\n        \"name\": \"__InputValue\"\n      },\n      {\n        \"name\": \"__EnumValue\"\n      },\n      {\n        \"name\": \"__Directive\"\n      }\n    ]\n  }\n}\n```\n\nWow, that's a lot of types! What are they? Let's group them:\n\n- **Query, Character, Human, Episode, Droid** - These are the ones that we\n  defined in our type system.\n- **String, Boolean** - These are built-in scalars that the type system\n  provided.\n- **`__Schema`, `__Type`, `__TypeKind`, `__Field`, `__InputValue`,\n  `__EnumValue`, `__Directive`** - These all are preceded with a double\n  underscore, indicating that they are part of the introspection system.\n\nNow, let's try and figure out a good place to start exploring what queries are\navailable. When we designed our type system, we specified what type all queries\nwould start at; let's ask the introspection system about that!\n\n```graphql\nquery IntrospectionQueryTypeQuery {\n  __schema {\n    queryType {\n      name\n    }\n  }\n}\n```\n\nand we get back:\n\n```json\n{\n  \"__schema\": {\n    \"queryType\": {\n      \"name\": \"Query\"\n    }\n  }\n}\n```\n\nAnd that matches what we said in the type system section, that the `Query` type\nis where we will start! Note that the naming here was just by convention; we\ncould have named our `Query` type anything else, and it still would have been\nreturned here if we had specified it as the starting type for queries. Naming it\n`Query`, though, is a useful convention.\n\nIt is often useful to examine one specific type. Let's take a look at the\n`Droid` type:\n\n```graphql\nquery IntrospectionDroidTypeQuery {\n  __type(name: \"Droid\") {\n    name\n  }\n}\n```\n\nand we get back:\n\n```json\n{\n  \"__type\": {\n    \"name\": \"Droid\"\n  }\n}\n```\n\nWhat if we want to know more about Droid, though? For example, is it an\ninterface or an object?\n\n```graphql\nquery IntrospectionDroidKindQuery {\n  __type(name: \"Droid\") {\n    name\n    kind\n  }\n}\n```\n\nand we get back:\n\n```json\n{\n  \"__type\": {\n    \"name\": \"Droid\",\n    \"kind\": \"OBJECT\"\n  }\n}\n```\n\n`kind` returns a `__TypeKind` enum, one of whose values is `OBJECT`. If we asked\nabout `Character` instead:\n\n```graphql\nquery IntrospectionCharacterKindQuery {\n  __type(name: \"Character\") {\n    name\n    kind\n  }\n}\n```\n\nand we get back:\n\n```json\n{\n  \"__type\": {\n    \"name\": \"Character\",\n    \"kind\": \"INTERFACE\"\n  }\n}\n```\n\nWe'd find that it is an interface.\n\nIt's useful for an object to know what fields are available, so let's ask the\nintrospection system about `Droid`:\n\n```graphql\nquery IntrospectionDroidFieldsQuery {\n  __type(name: \"Droid\") {\n    name\n    fields {\n      name\n      type {\n        name\n        kind\n      }\n    }\n  }\n}\n```\n\nand we get back:\n\n```json\n{\n  \"__type\": {\n    \"name\": \"Droid\",\n    \"fields\": [\n      {\n        \"name\": \"id\",\n        \"type\": {\n          \"name\": null,\n          \"kind\": \"NON_NULL\"\n        }\n      },\n      {\n        \"name\": \"name\",\n        \"type\": {\n          \"name\": \"String\",\n          \"kind\": \"SCALAR\"\n        }\n      },\n      {\n        \"name\": \"friends\",\n        \"type\": {\n          \"name\": null,\n          \"kind\": \"LIST\"\n        }\n      },\n      {\n        \"name\": \"appearsIn\",\n        \"type\": {\n          \"name\": null,\n          \"kind\": \"LIST\"\n        }\n      },\n      {\n        \"name\": \"primaryFunction\",\n        \"type\": {\n          \"name\": \"String\",\n          \"kind\": \"SCALAR\"\n        }\n      }\n    ]\n  }\n}\n```\n\nThose are our fields that we defined on `Droid`!\n\n`id` looks a bit weird there, it has no name for the type. That's because it's a\n\"wrapper\" type of kind `NON_NULL`. If we queried for `ofType` on that field's\ntype, we would find the `String` type there, telling us that this is a non-null\nString.\n\nSimilarly, both `friends` and `appearsIn` have no name, since they are the\n`LIST` wrapper type. We can query for `ofType` on those types, which will tell\nus what these are lists of.\n\n```graphql\nquery IntrospectionDroidWrappedFieldsQuery {\n  __type(name: \"Droid\") {\n    name\n    fields {\n      name\n      type {\n        name\n        kind\n        ofType {\n          name\n          kind\n        }\n      }\n    }\n  }\n}\n```\n\nand we get back:\n\n```json\n{\n  \"__type\": {\n    \"name\": \"Droid\",\n    \"fields\": [\n      {\n        \"name\": \"id\",\n        \"type\": {\n          \"name\": null,\n          \"kind\": \"NON_NULL\",\n          \"ofType\": {\n            \"name\": \"String\",\n            \"kind\": \"SCALAR\"\n          }\n        }\n      },\n      {\n        \"name\": \"name\",\n        \"type\": {\n          \"name\": \"String\",\n          \"kind\": \"SCALAR\",\n          \"ofType\": null\n        }\n      },\n      {\n        \"name\": \"friends\",\n        \"type\": {\n          \"name\": null,\n          \"kind\": \"LIST\",\n          \"ofType\": {\n            \"name\": \"Character\",\n            \"kind\": \"INTERFACE\"\n          }\n        }\n      },\n      {\n        \"name\": \"appearsIn\",\n        \"type\": {\n          \"name\": null,\n          \"kind\": \"LIST\",\n          \"ofType\": {\n            \"name\": \"Episode\",\n            \"kind\": \"ENUM\"\n          }\n        }\n      },\n      {\n        \"name\": \"primaryFunction\",\n        \"type\": {\n          \"name\": \"String\",\n          \"kind\": \"SCALAR\",\n          \"ofType\": null\n        }\n      }\n    ]\n  }\n}\n```\n\nLet's end with a feature of the introspection system particularly useful for\ntooling; let's ask the system for documentation!\n\n```graphql\nquery IntrospectionDroidDescriptionQuery {\n  __type(name: \"Droid\") {\n    name\n    description\n  }\n}\n```\n\nyields\n\n```json\n{\n  \"__type\": {\n    \"name\": \"Droid\",\n    \"description\": \"A mechanical creature in the Star Wars universe.\"\n  }\n}\n```\n\nSo we can access the documentation about the type system using introspection,\nand create documentation browsers, or rich IDE experiences.\n\nThis has just scratched the surface of the introspection system; we can query\nfor enum values, what interfaces a type implements, and more. We can even\nintrospect on the introspection system itself. The specification goes into more\ndetail about this topic in the \"Introspection\" section, and the\n[introspection](https://github.com/graphql/graphql-js/blob/main/src/type/introspection.ts)\nfile in GraphQL.js contains code implementing a specification-compliant GraphQL\nquery introspection system.\n\n### Additional Content\n\nThis README walked through the GraphQL.js reference implementation's type\nsystem, query execution, validation, and introspection systems. There's more in\nboth [GraphQL.js](https://github.com/graphql/graphql-js/) and specification,\nincluding a description and implementation for executing queries, how to format\na response, explaining how a type system maps to an underlying implementation,\nand how to format a GraphQL response, as well as the grammar for GraphQL.\n\n### Contributing to this repo\n\nThis repository is managed by EasyCLA. Project participants must sign the free\n([GraphQL Specification Membership agreement](https://preview-spec-membership.graphql.org)\nbefore making a contribution. You only need to do this one time, and it can be\nsigned by\n[individual contributors](https://individual-spec-membership.graphql.org/) or\ntheir [employers](https://corporate-spec-membership.graphql.org/).\n\nTo initiate the signature process please open a PR against this repo. The\nEasyCLA bot will block the merge if we still need a membership agreement from\nyou.\n\nYou can find\n[detailed information here](https://github.com/graphql/graphql-wg/tree/main/membership).\nIf you have issues, please email\n[operations@graphql.org](mailto:operations@graphql.org).\n\nIf your company benefits from GraphQL and you would like to provide essential\nfinancial support for the systems and people that power our community, please\nalso consider membership in the\n[GraphQL Foundation](https://foundation.graphql.org/join).\n","funding_links":[],"categories":["Shell","Others","Misc","JavaScript","Specifications","Resources","Uncategorized","GraphQL [🔝](#readme)"],"sub_categories":["API Specification","Uncategorized"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql%2Fgraphql-spec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphql%2Fgraphql-spec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphql%2Fgraphql-spec/lists"}