Please refer to below URL before starting Mutation
http://siddharathadhumale.blogspot.com/2019/07/creating-graphql-server-with-java.html
Lets say we want mutation for creation of User for that make the changes in schema.graphqls as shown below
type Mutation {
createUser(name: String!, age: Int!): User
}
In GraphQLProvider
private RuntimeWiring buildWiring() {
return RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Query")
.dataFetcher("userById", graphQLDataFetchers.getUserByIdDataFetcher()))
.type(newTypeWiring("User")
.dataFetcher("address", graphQLDataFetchers.getUserDataFetcher()))
//by siddhu for mutation start[
.type(newTypeWiring("Mutation")
.dataFetcher("createUser", graphQLDataFetchers.createUserDataFetcher()))
//by siddhu for mutation end]
.build();
}
In GraphQLDataFetchers
//by siddhu for Mutation start[
public DataFetcher createUserDataFetcher() {
System.out.println("here reached............");
return dataFetchingEnvironment -> {
String name = dataFetchingEnvironment.getArgument("name");
System.out.println("here reached..name.........."+name);
int age = dataFetchingEnvironment.getArgument("age");
System.out.println("here reached..age.........."+""+age);
Map<String, String> objMap4 = new HashMap();
objMap4.put("id", "sid-4");
objMap4.put("name", name);
objMap4.put("age", ""+age);
objMap4.put("addressId", "address-4");
users = Arrays.asList(objMap4);
return users
.stream()
.filter(user -> user.get("id").equals("sid-4"))
.findFirst()
.orElse(null);
//return users;
};
}
//by siddhu for Mutation end]
Please see below screen shot for execution of mutation
Image12
Note: you can download this code from below given locations
https://github.com/shdhumale/siddhu-user-details
http://siddharathadhumale.blogspot.com/2019/07/creating-graphql-server-with-java.html
Lets say we want mutation for creation of User for that make the changes in schema.graphqls as shown below
type Mutation {
createUser(name: String!, age: Int!): User
}
In GraphQLProvider
private RuntimeWiring buildWiring() {
return RuntimeWiring.newRuntimeWiring()
.type(newTypeWiring("Query")
.dataFetcher("userById", graphQLDataFetchers.getUserByIdDataFetcher()))
.type(newTypeWiring("User")
.dataFetcher("address", graphQLDataFetchers.getUserDataFetcher()))
//by siddhu for mutation start[
.type(newTypeWiring("Mutation")
.dataFetcher("createUser", graphQLDataFetchers.createUserDataFetcher()))
//by siddhu for mutation end]
.build();
}
In GraphQLDataFetchers
//by siddhu for Mutation start[
public DataFetcher createUserDataFetcher() {
System.out.println("here reached............");
return dataFetchingEnvironment -> {
String name = dataFetchingEnvironment.getArgument("name");
System.out.println("here reached..name.........."+name);
int age = dataFetchingEnvironment.getArgument("age");
System.out.println("here reached..age.........."+""+age);
Map<String, String> objMap4 = new HashMap();
objMap4.put("id", "sid-4");
objMap4.put("name", name);
objMap4.put("age", ""+age);
objMap4.put("addressId", "address-4");
users = Arrays.asList(objMap4);
return users
.stream()
.filter(user -> user.get("id").equals("sid-4"))
.findFirst()
.orElse(null);
//return users;
};
}
//by siddhu for Mutation end]
Please see below screen shot for execution of mutation
Image12
Note: you can download this code from below given locations
https://github.com/shdhumale/siddhu-user-details
No comments:
Post a Comment