Programmatically get all GraphQL schemes

Hi there!

I am currently working on an external project and I would like to know if it is possible to retrieve programmatically the different GraphQL schemes (queries, mutations) exposed by my Strapi project?

If I understood you correctly, you are asking about Introspection Queries.

For example, the following GraphQL syntax-text is asking the GraphQL server to return a list of all queries available to us from the schema:

{
  __schema {
    queryType {
      fields {
        name
      }
    }
  }
}

Returns all mutations available to us from the schema:

{
  __schema {
    mutationType {
      fields {
        name
      }
    }
  }
}

Returns the entire schema: queries, mutations, fields, etc.:

GraphQL Endpoint Introspection Query gist

query IntrospectionQuery {
    __schema {
      queryType { name }
      mutationType { name }
      subscriptionType { name }
      types {
        ...FullType
      }
      directives {
        name
        description
        args {
          ...InputValue
        }
        locations
      }
    }
  }
  fragment FullType on __Type {
    kind
    name
    description
    fields(includeDeprecated: true) {
      name
      description
      args {
        ...InputValue
      }
      type {
        ...TypeRef
      }
      isDeprecated
      deprecationReason
    }
    inputFields {
      ...InputValue
    }
    interfaces {
      ...TypeRef
    }
    enumValues(includeDeprecated: true) {
      name
      description
      isDeprecated
      deprecationReason
    }
    possibleTypes {
      ...TypeRef
    }
  }
  fragment InputValue on __InputValue {
    name
    description
    type { ...TypeRef }
    defaultValue
  }
  fragment TypeRef on __Type {
    kind
    name
    ofType {
      kind
      name
      ofType {
        kind
        name
        ofType {
          kind
          name
        }
      }
    }
  }

It would be perfect if there is a way to download programmatically all those Schema and Docs in order to create file for client-side Apollo request. It would be perfect because it’s a lake of time to create them by hand on all my frontend


We use https://www.graphql-code-generator.com/ currently to update latest changes on our frontend with Apollo client.
You can use previously mentioned Introspection query to download latest schema or even built-in GraphQL Code Generator feature to update the code.
You just need to configure code generation using provided docs codegen.yml | GraphQL Code Generator

Thanks,

Is it possible to generate all the mutations too ? Like Update, Delete, Find etc ??

Hi again!

I just try the graphql-codegen package and I try to use the typescript-generic-sdk plugin in order to build me a gql ready to use SDK :smiley: The problem is that when I try it on the GraphQL Code Generator live demo it works and it doesn’t when I use the schema which strapi expose :frowning:

Hello, is it possible to write what specific errors do not allow using the package? Let’s try to figure it out. We are working with the strapi 3.6.1 generated schema.

I don’t get any error. It just don’t generate the operations file & the sdk. My config is correct since It works on the live demo of GraphQL Code Generator.

Do you want I past you the types.ts which is generated by graphql-codegen ?