Java Code Examples for graphql.language.OperationDefinition#getOperation()

The following examples show how to use graphql.language.OperationDefinition#getOperation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GqlParentType.java    From manifold with Apache License 2.0 6 votes vote down vote up
private void addInnerOperations( SrcLinkedClass srcClass )
{
  for( OperationDefinition operation: _operations.values() )
  {
    switch( operation.getOperation() )
    {
      case QUERY:
      case MUTATION:
        // Queries and mutations are the same thing -- web requests
        addQueryType( operation, srcClass );
        break;
      case SUBSCRIPTION:
        // todo:
        break;
    }
  }
}
 
Example 2
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private void addFragmentValueMethod( SrcLinkedClass srcClass )
{
  if( !(_file instanceof IFileFragment) )
  {
    return;
  }

  switch( ((IFileFragment)_file).getHostKind() )
  {
    case DOUBLE_QUOTE_LITERAL:
    case TEXT_BLOCK_LITERAL:
      break;

    default:
      return;
  }

  for( OperationDefinition operation: _operations.values() )
  {
    switch( operation.getOperation() )
    {
      case QUERY:
      case MUTATION:
        // Queries and mutations are the same thing -- web requests
        addValueMethodForOperation( operation, srcClass );
        break;
      case SUBSCRIPTION:
        // todo:
        break;
    }
  }
}
 
Example 3
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
private GraphQLObjectType getRootType(GraphQLSchema schema, OperationDefinition operationDefinition) {
    if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) {
        return Objects.requireNonNull(schema.getMutationType());
    } else if (operationDefinition.getOperation() == OperationDefinition.Operation.QUERY) {
        return Objects.requireNonNull(schema.getQueryType());
    } else if (operationDefinition.getOperation() == OperationDefinition.Operation.SUBSCRIPTION) {
        return Objects.requireNonNull(schema.getSubscriptionType());
    } else {
        throw new IllegalStateException("Unknown operation type encountered. Incompatible graphql-java version?");
    }
}
 
Example 4
Source File: GraphQLAPIHandler.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public boolean handleRequest(MessageContext messageContext) {
    try {
        String payload;
        Parser parser = new Parser();

        org.apache.axis2.context.MessageContext axis2MC = ((Axis2MessageContext) messageContext).
                getAxis2MessageContext();
        String requestPath = messageContext.getProperty(REST_SUB_REQUEST_PATH).toString();
        if (requestPath != null && !requestPath.isEmpty()) {
            String[] queryParams = ((Axis2MessageContext) messageContext).getProperties().
                    get(REST_SUB_REQUEST_PATH).toString().split(QUERY_PATH_STRING);
            if (queryParams.length > 1) {
                payload = URLDecoder.decode(queryParams[1], UNICODE_TRANSFORMATION_FORMAT);
            } else {
                RelayUtils.buildMessage(axis2MC);
                OMElement body = axis2MC.getEnvelope().getBody().getFirstElement();
                if (body != null && body.getFirstElement() != null) {
                    payload = body.getFirstElement().getText();
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("Invalid query parameter " + queryParams[0]);
                    }
                    handleFailure(messageContext, "Invalid query parameter");
                    return false;
                }
            }
            messageContext.setProperty(APIConstants.GRAPHQL_PAYLOAD, payload);
        } else {
            handleFailure(messageContext, "Request path cannot be empty");
            return false;
        }

        // Validate payload with graphQLSchema
        Document document = parser.parseDocument(payload);

        if (validatePayloadWithSchema(messageContext, document)) {
            supportForBasicAndAuthentication(messageContext);

            // Extract the operation type and operations from the payload
            for (Definition definition : document.getDefinitions()) {
                if (definition instanceof OperationDefinition) {
                    OperationDefinition operation = (OperationDefinition) definition;
                    if (operation.getOperation() != null) {
                        String httpVerb = ((Axis2MessageContext) messageContext).getAxis2MessageContext().
                                getProperty(HTTP_METHOD).toString();
                        messageContext.setProperty(HTTP_VERB, httpVerb);
                        ((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(HTTP_METHOD,
                                operation.getOperation().toString());
                        String operationList = getOperationList(messageContext, operation);
                        messageContext.setProperty(APIConstants.API_ELECTED_RESOURCE, operationList);
                        if (log.isDebugEnabled()) {
                            log.debug("Operation list has been successfully added to elected property");
                        }
                        return true;
                    }
                } else {
                    handleFailure(messageContext, "Operation definition cannot be empty");
                    return false;
                }
            }
        } else {
            return false;
        }
    } catch (IOException | XMLStreamException | InvalidSyntaxException e) {
        log.error(e.getMessage());
        handleFailure(messageContext, e.getMessage());
    }
    return false;
}