Search code examples
graphqlgraphql-jsintrospection

Filter or distinguish built-in and custom graphql types


I'm trying to find a way to distinguish (and filter out) the built-in types from my custom types when running an introspectionQuery against a graphql api. There doesn't seem to be anything reliable in the output to identify which types are built-in (apart from the __ in front of the "system" types).

At this point I can't even seem to find a single official list, so my best option seems to be to go over the introspectionQuery output and make a list for future use, hoping nothing changes.

Are the really no systematic way to distinguish the two?


Solution

  • You can use the isIntrospectionType function:

    import { buildSchema, isIntrospectionType } from 'graphql';
    
    
    const schema = buildSchema(source);
    
    for (const type of Object.values(schema.getTypeMap())) {
      if (isIntrospectionType(type)) continue;
    
      // do something...
    }
    

    PS. You can use isSpecifiedScalarType as well to filter Scalar nodes such as ID, Int, etc..