graphql.language.SchemaDefinition Java Examples

The following examples show how to use graphql.language.SchemaDefinition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GqlParentType.java    From manifold with Apache License 2.0 6 votes vote down vote up
private TypeDefinition getRoot( OperationDefinition.Operation operation )
{
  TypeDefinition root = null;
  SchemaDefinition schemaDefinition = findSchemaDefinition();
  if( schemaDefinition != null )
  {
    Optional<OperationTypeDefinition> rootQueryType =
      schemaDefinition.getOperationTypeDefinitions().stream()
        .filter( e -> e.getName().equals( getOperationKey( operation ) ) ).findFirst();
    if( rootQueryType.isPresent() )
    {
      Type type = rootQueryType.get().getTypeName();
      root = findTypeDefinition( type );
    }
  }
  if( root == null )
  {
    // e.g., by convention a 'type' named "Query" is considered the root query type
    // if one is not specified in the 'schema'
    root = _gqlManifold.findTypeDefinition( getOperationDefaultTypeName( operation ) );
  }
  return root;
}
 
Example #2
Source File: TypeEntry.java    From graphql-apigen with Apache License 2.0 6 votes vote down vote up
private static List<Directive> getDirectives(Definition def) {
    if ( def instanceof ObjectTypeDefinition ) {
        return ((ObjectTypeDefinition)def).getDirectives();
    } if ( def instanceof InterfaceTypeDefinition ) {
        return ((InterfaceTypeDefinition)def).getDirectives();
    } if ( def instanceof EnumTypeDefinition ) {
        return ((EnumTypeDefinition)def).getDirectives();
    } if ( def instanceof ScalarTypeDefinition ) {
        return ((ScalarTypeDefinition)def).getDirectives();
    } if ( def instanceof UnionTypeDefinition ) {
        return ((UnionTypeDefinition)def).getDirectives();
    } if ( def instanceof InputObjectTypeDefinition ) {
        return ((InputObjectTypeDefinition)def).getDirectives();
    } if ( def instanceof SchemaDefinition ) {
        return ((SchemaDefinition)def).getDirectives();
    }
    return Collections.emptyList();
}
 
Example #3
Source File: STModel.java    From graphql-apigen with Apache License 2.0 6 votes vote down vote up
public synchronized List<String> getImports() {
    if ( null == imports ) {
        Definition def = typeEntry.getDefinition();
        Set<String> names = new TreeSet<String>();
        if ( isObjectType() ) {
            addImports(names, (ObjectTypeDefinition)def);
        } else if ( isInterfaceType() ) {
            addImports(names, (InterfaceTypeDefinition)def);
        } else if ( isInputObjectType() ) {
            addImports(names, (InputObjectTypeDefinition)def);
        } else if ( isUnionType() ) {
            addImports(names, (UnionTypeDefinition)def);
        } else if ( isEnumType() ) {
            addImports(names, (EnumTypeDefinition)def);
        } else if ( isSchemaType() ) {
            addImports(names, (SchemaDefinition)def);
        }
        imports = new ArrayList<>(names);
    }
    return imports;
}
 
Example #4
Source File: STModel.java    From graphql-apigen with Apache License 2.0 6 votes vote down vote up
public synchronized List<Field> getFields() {
    if ( null == fields ) {
        Definition def = typeEntry.getDefinition();
        if ( isObjectType() ) {
            fields = getFields((ObjectTypeDefinition)def);
        } else if ( isInterfaceType() ) {
            fields = getFields((InterfaceTypeDefinition)def);
        } else if ( isInputObjectType() ) {
            fields = getFields((InputObjectTypeDefinition)def);
        } else if ( isUnionType() ) {
            fields = getFields((UnionTypeDefinition)def);
        } else if ( isEnumType() ) {
            fields = getFields((EnumTypeDefinition)def);
        } else if ( isSchemaType() ) {
            fields = getFields((SchemaDefinition)def);
        } else {
            fields = Collections.emptyList();
        }
    }
    return fields;
}
 
Example #5
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
GqlParentType( String fqn, SchemaDefinition schemaDefinition, TypeDefinitionRegistry registry,
               Map<String, OperationDefinition> operations, Map<String, FragmentDefinition> fragments,
               IFile file, GqlManifold gqlManifold )
{
  _fqn = fqn;
  _schemaDefinition = schemaDefinition;
  _registry = registry;
  _operations = operations;
  _fragments = fragments;
  _file = file;
  _gqlManifold = gqlManifold;
  _typeToUnions = new HashMap<>();
}
 
Example #6
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
/**
 * Searches globally for the schema definition i.e., across all .graphql files.
 * Assuming only one is defined. todo: verify if 'schema' s/b defined only once
 */
private SchemaDefinition findSchemaDefinition()
{
  if( _schemaDefinition != null )
  {
    return _schemaDefinition;
  }

  return _gqlManifold.findSchemaDefinition();
}
 
Example #7
Source File: GqlModel.java    From manifold with Apache License 2.0 5 votes vote down vote up
private void buildRegistry( Document document )
{
  TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
  List<Definition> definitions = document.getDefinitions();
  Map<String, OperationDefinition> operations = new LinkedHashMap<>();
  Map<String, FragmentDefinition> fragments = new LinkedHashMap<>();
  List<GraphQLError> errors = new ArrayList<>();
  for( Definition definition: definitions )
  {
    if( definition instanceof SchemaDefinition )
    {
      _schemaDefinition = (SchemaDefinition)definition;
    }
    else if( definition instanceof SDLDefinition )
    {
      // types, interfaces, unions, inputs, scalars, extensions
      typeRegistry.add( (SDLDefinition)definition ).ifPresent( errors::add );
      if( definition instanceof ScalarTypeDefinition )
      {
        // register scalar type
        typeRegistry.scalars().put( ((ScalarTypeDefinition)definition).getName(), (ScalarTypeDefinition)definition );
      }
    }
    else if( definition instanceof OperationDefinition )
    {
      // queries, mutations, subscriptions
      operations.put( ((OperationDefinition)definition).getName(), (OperationDefinition)definition );
    }
    else if( definition instanceof FragmentDefinition )
    {
      // fragments
      fragments.put( ((FragmentDefinition)definition).getName(), (FragmentDefinition)definition );
    }
  }
  _issues = new GqlIssueContainer( errors, getFile() );
  _typeRegistry = typeRegistry;
  _operations = operations;
  _fragments = fragments;
}
 
Example #8
Source File: GqlManifold.java    From manifold with Apache License 2.0 5 votes vote down vote up
SchemaDefinition findSchemaDefinition()
{
  return getAllTypeNames().stream()
    .map( fqn -> {
      GqlModel model = getModel( fqn );
      return model == null ? null : model.getSchemaDefinition();
    } )
    .filter( Objects::nonNull )
    .findFirst().orElse( null );
}
 
Example #9
Source File: ApiGen.java    From graphql-apigen with Apache License 2.0 5 votes vote down vote up
private void add(Map<String, TypeEntry> types, URL path) throws IOException {
    String content = slurp(path);
    try {
        Document doc = parser.parseDocument(content);
        for ( Definition definition : doc.getDefinitions() ) {
            if ( definition instanceof SchemaDefinition ) {
                if ( generatedTypes == types ) {
                    schemaDefinitions.add(new TypeEntry(definition, path, defaultPackageName));
                }
                continue;
            } else if ( ! (definition instanceof TypeDefinition) ) {
                // TODO: What about @definition types?
                throw new RuntimeException(
                    "GraphQL schema documents must only contain schema type definitions, got "+
                    definition.getClass().getSimpleName() + " [" +
                    definition.getSourceLocation().getLine() + "," +
                    definition.getSourceLocation().getColumn() + "]");
            }
            TypeEntry newEntry = new TypeEntry(definition, path, defaultPackageName);
            TypeEntry oldEntry = referenceTypes.get(newEntry.getName());

            if ( null != oldEntry ) {
                // TODO: Support the extend type?
                throw new RuntimeException(
                    "Duplicate type definition for '" + newEntry.getName() + "'" +
                    " defined both in " + oldEntry.getSourceLocation() + " and " +
                    newEntry.getSourceLocation());
            }

            types.put(newEntry.getName(), newEntry);
            if ( types != referenceTypes ) {
                // All types should be added to reference types...
                referenceTypes.put(newEntry.getName(), newEntry);
            }
        }
    } catch ( Exception ex ) {
        throw new RuntimeException(ex.getMessage() + " when parsing '"+path+"'", ex);
    }
}
 
Example #10
Source File: STModel.java    From graphql-apigen with Apache License 2.0 5 votes vote down vote up
private List<Field> getFields(SchemaDefinition def) {
    List<Field> fields = new ArrayList<Field>();
    for ( OperationTypeDefinition fieldDef : def.getOperationTypeDefinitions() ) {
        fields.add(new Field(fieldDef.getName(), toJavaTypeName(fieldDef.getTypeName())));
    }
    return fields;
}
 
Example #11
Source File: GqlModel.java    From manifold with Apache License 2.0 4 votes vote down vote up
SchemaDefinition getSchemaDefinition()
{
  return _schemaDefinition;
}
 
Example #12
Source File: STModel.java    From graphql-apigen with Apache License 2.0 4 votes vote down vote up
public boolean isSchemaType() {
    return typeEntry.getDefinition() instanceof SchemaDefinition;
}
 
Example #13
Source File: STModel.java    From graphql-apigen with Apache License 2.0 4 votes vote down vote up
private void addImports(Collection<String> imports, SchemaDefinition def) {
    for ( OperationTypeDefinition fieldDef : def.getOperationTypeDefinitions() ) {
        addImports(imports, fieldDef.getTypeName());
    }
}
 
Example #14
Source File: GraphQLIntrospectionResultToSchema.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
/**
 * Returns a IDL Document that represents the schema as defined by the introspection result map
 *
 * @param introspectionResult the result of an introspection query on a schema
 *
 * @return a IDL Document of the schema
 */
@SuppressWarnings("unchecked")
public Document createSchemaDefinition(Map<String, Object> introspectionResult) {
    assertTrue(introspectionResult.get("__schema") != null, () -> "__schema expected");
    Map<String, Object> schema = (Map<String, Object>) introspectionResult.get("__schema");


    Map<String, Object> queryType = (Map<String, Object>) schema.get("queryType");
    assertNotNull(queryType, () -> "queryType expected");
    TypeName query = TypeName.newTypeName().name((String) queryType.get("name")).build();
    boolean nonDefaultQueryName = !"Query".equals(query.getName());

    SchemaDefinition.Builder schemaDefinition = SchemaDefinition.newSchemaDefinition();
    schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("query").typeName(query).build());

    Map<String, Object> mutationType = (Map<String, Object>) schema.get("mutationType");
    boolean nonDefaultMutationName = false;
    if (mutationType != null) {
        TypeName mutation = TypeName.newTypeName().name((String) mutationType.get("name")).build();
        nonDefaultMutationName = !"Mutation".equals(mutation.getName());
        schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("mutation").typeName(mutation).build());
    }

    Map<String, Object> subscriptionType = (Map<String, Object>) schema.get("subscriptionType");
    boolean nonDefaultSubscriptionName = false;
    if (subscriptionType != null) {
        TypeName subscription = TypeName.newTypeName().name(((String) subscriptionType.get("name"))).build();
        nonDefaultSubscriptionName = !"Subscription".equals(subscription.getName());
        schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("subscription").typeName(subscription).build());
    }

    Document.Builder document = Document.newDocument();
    if (nonDefaultQueryName || nonDefaultMutationName || nonDefaultSubscriptionName) {
        document.definition(schemaDefinition.build());
    }

    List<Map<String, Object>> types = (List<Map<String, Object>>) schema.get("types");
    for (Map<String, Object> type : types) {
        TypeDefinition typeDefinition = createTypeDefinition(type);
        if (typeDefinition == null) continue;
        document.definition(typeDefinition);
    }

    return document.build();
}