Created
May 4, 2017 04:55
-
-
Save ieugen/d820b95b72bf34364fb519c80245c505 to your computer and use it in GitHub Desktop.
Graphql-apigen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class Account implements gr8pi.graphql.core.api.Account { | |
@Override | |
public String getId() { | |
return "demo"; | |
} | |
@Override | |
public List<Respondent> getRespondents() { | |
return Collections.EMPTY_LIST; | |
} | |
@Override | |
public List<User> getUsers() { | |
return Collections.EMPTY_LIST; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Slf4j | |
@Component | |
public class AccountResolver implements Account.Resolver { | |
public AccountResolver() { | |
log.info("Creating account resolver"); | |
} | |
@Override | |
public List<Account> resolve(List<Account> list) { | |
return Arrays.asList(new Account.Builder() | |
.withId("1") | |
.withRespondents(Collections.EMPTY_LIST) | |
.withUsers(Collections.EMPTY_LIST) | |
.build()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gr8pi.graphql.core; | |
import com.google.common.collect.Maps; | |
import gr8pi.graphql.core.config.GraphqlConfig; | |
import graphql.ExecutionResult; | |
import graphql.GraphQL; | |
import lombok.AccessLevel; | |
import lombok.experimental.FieldDefaults; | |
import org.hamcrest.CoreMatchers; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import java.util.Map; | |
import static org.junit.Assert.assertNotNull; | |
@FieldDefaults(level = AccessLevel.PRIVATE) | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(classes = {GraphqlConfig.class}) | |
public class GraphqlBootstrapTest { | |
@Autowired | |
GraphQL graphQL; | |
@Test | |
public void bootstrapGraphql() throws Exception { | |
assertNotNull(graphQL); | |
Map<String, Object> context = Maps.newHashMap(); | |
context.put("hello", "world"); | |
ExecutionResult result = graphQL.execute("{accounts {id}}", context); | |
Assert.assertThat(result, CoreMatchers.notNullValue()); | |
Object data = result.getData(); | |
Assert.assertThat(data, CoreMatchers.notNullValue()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gr8pi.graphql.core.config; | |
import graphql.GraphQL; | |
import graphql.execution.SimpleExecutionStrategy; | |
import graphql.schema.GraphQLObjectType; | |
import graphql.schema.GraphQLSchema; | |
import graphql.schema.GraphQLType; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import javax.inject.Provider; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
@ComponentScan("gr8pi.graphql.core") | |
@Configuration | |
public class GraphqlConfig { | |
@Bean | |
public Map<String, GraphQLType> graphqlTypeMap(List<Provider<? extends GraphQLType>> typeList) { | |
return typeList.stream() | |
.map(Provider::get) | |
.collect(Collectors.toMap(GraphQLType::getName, Function.identity())); | |
} | |
@Bean | |
@Autowired | |
public GraphQLSchema graphQLSchema(Map<String, GraphQLType> types) { | |
GraphQLSchema schema = GraphQLSchema.newSchema() | |
.query((GraphQLObjectType) types.get("QueryAccounts")) | |
.build(new HashSet<>(types.values())); | |
return schema; | |
} | |
@Bean | |
@Autowired | |
public GraphQL graphQL(GraphQLSchema schema) { | |
GraphQL graphQL = GraphQL.newGraphQL(schema) | |
.queryExecutionStrategy(new SimpleExecutionStrategy()) | |
.mutationExecutionStrategy(new SimpleExecutionStrategy()) | |
.build(); | |
return graphQL; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Account @java(package:"gr8pi.graphql.core.api") { | |
id: ID! | |
respondents: [Respondent]! | |
users: [User]! | |
} | |
type Respondent @java(package:"gr8pi.graphql.core.api"){ | |
id: ID! | |
email: String! | |
firstName: String | |
lastName: String | |
} | |
type User @java(package:"gr8pi.graphql.core.api"){ | |
id: ID! | |
email: String! | |
firstName: String | |
lastName: String | |
} | |
type Assessment @java(package:"gr8pi.graphql.core.api") { | |
id: ID! | |
respondent: Respondent! | |
} | |
type QueryAccounts @java(package:"gr8pi.graphql.core.api") { | |
accounts: [Account!] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment