RuntimeWiringbuildRuntimeWiring(){returnRuntimeWiring.newRuntimeWiring().scalar(CustomScalar)// this uses builder function lambda syntax
.type("QueryType",typeWiring->typeWiring.dataFetcher("hero",newStaticDataFetcher(StarWarsData.getArtoo())).dataFetcher("human",StarWarsData.getHumanDataFetcher()).dataFetcher("droid",StarWarsData.getDroidDataFetcher())).type("Human",typeWiring->typeWiring.dataFetcher("friends",StarWarsData.getFriendsDataFetcher()))// you can use builder syntax if you don't like the lambda syntax
.type("Droid",typeWiring->typeWiring.dataFetcher("friends",StarWarsData.getFriendsDataFetcher()))// or full builder syntax if that takes your fancy
.type(newTypeWiring("Character").typeResolver(StarWarsData.getCharacterTypeResolver()).build()).build();}
DataFetcher<Foo>fooDataFetcher=newDataFetcher<Foo>(){@OverridepublicFooget(DataFetchingEnvironmentenvironment){// environment.getSource() is the value of the surrounding
// object. In this case described by objectType
Foovalue=perhapsFromDatabase();// Perhaps getting from a DB or whatever
returnvalue;}};GraphQLObjectTypeobjectType=newObject().name("ObjectType").field(newFieldDefinition().name("foo").type(GraphQLString)).build();GraphQLCodeRegistrycodeRegistry=newCodeRegistry().dataFetcher(coordinates("ObjectType","foo"),fooDataFetcher).build();
//Simpson应该是卡通片《辛普森一家》中的一类怪物
type SimpsonCharacter {
name: String
mainCharacter: Boolean //是否是主角
}
Java Example:
1
2
3
4
5
6
7
8
9
10
11
12
GraphQLObjectTypesimpsonCharacter=newObject().name("SimpsonCharacter").description("A Simpson character").field(newFieldDefinition().name("name").description("The name of the character.").type(GraphQLString)).field(newFieldDefinition().name("mainCharacter").description("One of the main Simpson characters?").type(GraphQLBoolean)).build();
Interface
Interfaces 是类型的抽象定义,哇塞!
SDL Example:
1
2
3
4
//滑稽角色
interface ComicCharacter {
name: String;
}
Java Example:
1
2
3
4
5
6
7
8
GraphQLInterfaceTypecomicCharacter=newInterface().name("ComicCharacter").description("An abstract comic character.").field(newFieldDefinition().name("name").description("The name of the character.").type(GraphQLString)).build();
Union
SDL Example:
1
2
3
4
5
6
7
8
9
10
11
type Cat {
name: String;
lives: Int;
}
type Dog {
name: String;
bonesOwned: int;
}
union Pet = Cat | Dog
SchemaParserschemaParser=newSchemaParser();SchemaGeneratorschemaGenerator=newSchemaGenerator();FileschemaFile1=loadSchema("starWarsSchemaPart1.graphqls");FileschemaFile2=loadSchema("starWarsSchemaPart2.graphqls");FileschemaFile3=loadSchema("starWarsSchemaPart3.graphqls");TypeDefinitionRegistrytypeRegistry=newTypeDefinitionRegistry();// each registry is merged into the main registry
typeRegistry.merge(schemaParser.parse(schemaFile1));typeRegistry.merge(schemaParser.parse(schemaFile2));typeRegistry.merge(schemaParser.parse(schemaFile3));GraphQLSchemagraphQLSchema=schemaGenerator.makeExecutableSchema(typeRegistry,buildRuntimeWiring());
schema {
query: CombinedQueryFromMultipleTeams
}
type CombinedQueryFromMultipleTeams {
createdTimestamp: String
}
# maybe the invoicing system team puts in this set of attributes
extend type CombinedQueryFromMultipleTeams {
invoicing: Invoicing
}
# and the billing system team puts in this set of attributes
extend type CombinedQueryFromMultipleTeams {
billing: Billing
}
# and so and so forth
extend type CombinedQueryFromMultipleTeams {
auditing: Auditing
}