use of graphql.schema.idl.SchemaParser in project carbon-apimgt by wso2.
the class GraphQLRequestProcessorTest method testHandleRequestSuccess.
@Test
public void testHandleRequestSuccess() throws Exception {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
int msgSize = 100;
String msgText = "{\"id\":\"1\",\"type\":\"start\",\"payload\":{\"variables\":{},\"extensions\":{}," + "\"operationName\":null,\"query\":\"subscription {\\n " + "liftStatusChange {\\n id\\n name\\n }\\n}\\n\"}}";
PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
PowerMockito.when(InboundWebsocketProcessorUtil.authenticateToken(inboundMessageContext)).thenReturn(responseDTO);
// Get schema and parse
String graphqlDirPath = "graphQL" + File.separator;
String relativePath = graphqlDirPath + "schema_with_additional_props.graphql";
String schemaString = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(relativePath));
SchemaParser schemaParser = new SchemaParser();
TypeDefinitionRegistry registry = schemaParser.parse(schemaString);
GraphQLSchema schema = UnExecutableSchemaGenerator.makeUnExecutableSchema(registry);
GraphQLSchemaDTO schemaDTO = new GraphQLSchemaDTO(schema, registry);
inboundMessageContext.setGraphQLSchemaDTO(schemaDTO);
PowerMockito.when(InboundWebsocketProcessorUtil.validateScopes(inboundMessageContext, "liftStatusChange", "1")).thenReturn(responseDTO);
VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
verbInfoDTO.setHttpVerb("SUBSCRIPTION");
verbInfoDTO.setThrottling("Unlimited");
PowerMockito.when(InboundWebsocketProcessorUtil.findMatchingVerb("liftStatusChange", inboundMessageContext)).thenReturn(verbInfoDTO);
APIKeyValidationInfoDTO infoDTO = new APIKeyValidationInfoDTO();
infoDTO.setGraphQLMaxComplexity(4);
infoDTO.setGraphQLMaxDepth(3);
inboundMessageContext.setInfoDTO(infoDTO);
PowerMockito.when(InboundWebsocketProcessorUtil.doThrottleForGraphQL(msgSize, verbInfoDTO, inboundMessageContext, "1")).thenReturn(responseDTO);
GraphQLRequestProcessor graphQLRequestProcessor = new GraphQLRequestProcessor();
InboundProcessorResponseDTO processorResponseDTO = graphQLRequestProcessor.handleRequest(msgSize, msgText, inboundMessageContext);
Assert.assertFalse(processorResponseDTO.isError());
Assert.assertNull(processorResponseDTO.getErrorMessage());
Assert.assertEquals(inboundMessageContext.getVerbInfoForGraphQLMsgId("1").getOperation(), "liftStatusChange");
Assert.assertEquals(inboundMessageContext.getVerbInfoForGraphQLMsgId("1").getVerbInfoDTO().getHttpVerb(), "SUBSCRIPTION");
Assert.assertEquals(inboundMessageContext.getVerbInfoForGraphQLMsgId("1").getVerbInfoDTO().getThrottling(), "Unlimited");
}
use of graphql.schema.idl.SchemaParser in project carbon-apimgt by wso2.
the class GraphQLSchemaDefinition method extractGraphQLTypeList.
/**
* Extract GraphQL Types and Fields from given schema
*
* @param schema GraphQL Schema
* @return list of all types and fields
*/
public List<GraphqlSchemaType> extractGraphQLTypeList(String schema) {
List<GraphqlSchemaType> typeList = new ArrayList<>();
SchemaParser schemaParser = new SchemaParser();
TypeDefinitionRegistry typeRegistry = schemaParser.parse(schema);
Map<java.lang.String, TypeDefinition> list = typeRegistry.types();
for (Map.Entry<String, TypeDefinition> entry : list.entrySet()) {
if (entry.getValue() instanceof ObjectTypeDefinition) {
GraphqlSchemaType graphqlSchemaType = new GraphqlSchemaType();
List<String> fieldList = new ArrayList<>();
graphqlSchemaType.setType(entry.getValue().getName());
for (FieldDefinition fieldDef : ((ObjectTypeDefinition) entry.getValue()).getFieldDefinitions()) {
fieldList.add(fieldDef.getName());
}
graphqlSchemaType.setFieldList(fieldList);
typeList.add(graphqlSchemaType);
}
}
return typeList;
}
use of graphql.schema.idl.SchemaParser in project carbon-apimgt by wso2.
the class GraphQLAPIHandlerTest method setup.
@Before
public void setup() throws IOException {
messageContext = Mockito.mock(Axis2MessageContext.class);
axis2MessageContext = Mockito.mock(org.apache.axis2.context.MessageContext.class);
omElement = Mockito.mock(OMElement.class);
dataHolder = Mockito.mock(DataHolder.class);
SOAPEnvelope soapEnvelope = Mockito.mock(SOAPEnvelope.class);
SOAPBody soapBody = Mockito.mock(SOAPBody.class);
PowerMockito.mockStatic(DataHolder.class);
OMElement body = Mockito.mock(OMElement.class);
Map propertyList = Mockito.mock(Map.class);
Mockito.when(messageContext.getAxis2MessageContext()).thenReturn(axis2MessageContext);
Mockito.when(axis2MessageContext.getIncomingTransportName()).thenReturn("ws");
Mockito.when(messageContext.getProperty(APIConstants.GRAPHQL_SUBSCRIPTION_REQUEST)).thenReturn(true);
Mockito.when(axis2MessageContext.getIncomingTransportName()).thenReturn("wss");
Mockito.when(axis2MessageContext.getEnvelope()).thenReturn(soapEnvelope);
Mockito.when(soapEnvelope.getBody()).thenReturn(soapBody);
Mockito.when(soapBody.getFirstElement()).thenReturn(body);
Mockito.when(body.getFirstChildWithName(QName.valueOf("query"))).thenReturn(omElement);
Mockito.when(messageContext.getProperties()).thenReturn(propertyList);
Mockito.when(messageContext.getProperty(REST_SUB_REQUEST_PATH)).thenReturn("/");
Mockito.when(propertyList.get(REST_SUB_REQUEST_PATH)).thenReturn("/");
Mockito.when(propertyList.get(REST_SUB_REQUEST_PATH).toString().split("/?query=")).thenReturn(new String[0]);
Mockito.when(DataHolder.getInstance()).thenReturn(dataHolder);
// Get schema and parse
schemaDTOMap = new HashMap<>();
String graphqlDirPath = "graphQL" + File.separator;
String relativePath = graphqlDirPath + "schema_with_additional_props.graphql";
String schemaString = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(relativePath));
SchemaParser schemaParser = new SchemaParser();
TypeDefinitionRegistry registry = schemaParser.parse(schemaString);
GraphQLSchema schema = UnExecutableSchemaGenerator.makeUnExecutableSchema(registry);
GraphQLSchemaDTO schemaDTO = new GraphQLSchemaDTO(schema, registry);
schemaDTOMap.put("12345", schemaDTO);
Mockito.when(dataHolder.getApiToGraphQLSchemaDTOMap()).thenReturn(schemaDTOMap);
}
use of graphql.schema.idl.SchemaParser in project carbon-apimgt by wso2.
the class GraphqlQueryAnalysisMappingUtil method fromDTOtoValidatedGraphqlComplexityInfo.
/**
* Converts a GraphQLQueryComplexityInfo DTO object into a GraphqlComplexityInfo object. During this process a
* basic validation is done comparing with the types of the schema
*
* @param graphQLQueryComplexityInfoDTO GraphQLQueryComplexityInfoDTO object
* @param schema GraphQL Schema
* @return a new GraphqlComplexityInfo object corresponding to given GraphQLQueryComplexityInfoDTO object
*/
public static GraphqlComplexityInfo fromDTOtoValidatedGraphqlComplexityInfo(GraphQLQueryComplexityInfoDTO graphQLQueryComplexityInfoDTO, String schema) {
SchemaParser schemaParser = new SchemaParser();
Set<String> complexityInfoTypeSet = schemaParser.parse(schema).types().keySet();
GraphqlComplexityInfo graphqlComplexityInfo = new GraphqlComplexityInfo();
List<CustomComplexityDetails> customComplexityDetailsList = new ArrayList<CustomComplexityDetails>();
for (GraphQLCustomComplexityInfoDTO graphQLCustomComplexityInfoDTO : graphQLQueryComplexityInfoDTO.getList()) {
String complexityType = graphQLCustomComplexityInfoDTO.getType();
if (complexityInfoTypeSet.contains(complexityType)) {
CustomComplexityDetails customComplexityDetails = new CustomComplexityDetails();
customComplexityDetails.setType(complexityType);
customComplexityDetails.setField(graphQLCustomComplexityInfoDTO.getField());
customComplexityDetails.setComplexityValue(graphQLCustomComplexityInfoDTO.getComplexityValue());
customComplexityDetailsList.add(customComplexityDetails);
} else {
log.error("Complexity Type : " + complexityType + " is not included in the original schema. Hence " + "skipped.");
}
}
graphqlComplexityInfo.setList(customComplexityDetailsList);
return graphqlComplexityInfo;
}
use of graphql.schema.idl.SchemaParser in project graphql-java by graphql-java.
the class HttpMain method buildStarWarsSchema.
private GraphQLSchema buildStarWarsSchema() {
//
if (starWarsSchema == null) {
//
//
// the fetcher of friends uses java-dataloader to make the circular friends fetching
// more efficient by batching and caching the calls to load Character friends
//
DataFetcher friendsFetcher = environment -> {
DataLoaderRegistry dataloaderRegistry = asMapGet(environment.getContext(), "dataloaderRegistry");
DataLoader friendsDataLoader = dataloaderRegistry.getDataLoader("friends");
List<String> friendIds = asMapGet(environment.getSource(), "friends");
return friendsDataLoader.loadMany(friendIds);
};
//
// reads a file that provides the schema types
//
Reader streamReader = loadSchemaFile("starWarsSchemaAnnotated.graphqls");
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(streamReader);
//
// the runtime wiring is used to provide the code that backs the
// logical schema
//
TypeResolver characterTypeResolver = env -> {
Map<String, Object> obj = (Map<String, Object>) env.getObject();
String id = (String) obj.get("id");
GraphQLSchema schema = env.getSchema();
if (StarWarsData.isHuman(id)) {
return (GraphQLObjectType) schema.getType("Human");
} else {
return (GraphQLObjectType) schema.getType("Droid");
}
};
RuntimeWiring wiring = RuntimeWiring.newRuntimeWiring().type(newTypeWiring("Query").dataFetcher("hero", StarWarsData.getHeroDataFetcher()).dataFetcher("human", StarWarsData.getHumanDataFetcher()).dataFetcher("droid", StarWarsData.getDroidDataFetcher())).type(newTypeWiring("Human").dataFetcher("friends", friendsFetcher)).type(newTypeWiring("Droid").dataFetcher("friends", friendsFetcher)).type(newTypeWiring("Character").typeResolver(characterTypeResolver)).type(newTypeWiring("Episode").enumValues(StarWarsData.getEpisodeResolver())).build();
// finally combine the logical schema with the physical runtime
starWarsSchema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring);
}
return starWarsSchema;
}
Aggregations