// Extracts the root type of the operation from the schema. func getOperationRootType(schema types.GraphQLSchema, operation ast.Definition, r chan *types.GraphQLResult) (objType *types.GraphQLObjectType) { if operation == nil { var result types.GraphQLResult err := graphqlerrors.NewGraphQLFormattedError("Can only execute queries and mutations") result.Errors = append(result.Errors, err) r <- &result return objType } switch operation.GetOperation() { case "query": return schema.GetQueryType() case "mutation": mutationType := schema.GetMutationType() if mutationType.Name == "" { var result types.GraphQLResult err := graphqlerrors.NewGraphQLFormattedError("Schema is not configured for mutations") result.Errors = append(result.Errors, err) r <- &result return objType } return mutationType default: var result types.GraphQLResult err := graphqlerrors.NewGraphQLFormattedError("Can only execute queries and mutations") result.Errors = append(result.Errors, err) r <- &result return objType } }
/** * This method looks up the field on the given type defintion. * It has special casing for the two introspection fields, __schema * and __typename. __typename is special because it can always be * queried as a field, even in situations where no other fields * are allowed, like on a Union. __schema could get automatically * added to the query type, but that would require mutating type * definitions, which would cause issues. */ func getFieldDef(schema types.GraphQLSchema, parentType *types.GraphQLObjectType, fieldName string) *types.GraphQLFieldDefinition { if parentType == nil { return nil } if fieldName == types.SchemaMetaFieldDef.Name && schema.GetQueryType() == parentType { return types.SchemaMetaFieldDef } if fieldName == types.TypeMetaFieldDef.Name && schema.GetQueryType() == parentType { return types.TypeMetaFieldDef } if fieldName == types.TypeNameMetaFieldDef.Name { return types.TypeNameMetaFieldDef } return parentType.GetFields()[fieldName] }