GraphQL query validation with schema which we fetch using get_introspection_schema method

21 Views Asked by At

details are below

Please help me in getting solution on validating the query which we send via code vs the schema or anyway validating the query before hitting the API The problem I am facing is when I use Validate function with schema & query its not vaildating the fields is there any other way to validate query which we have written with the schema? I have tried graphql-core library also but nothing is working as expected

import json

from graphql.validation.validate import validate

from graphql.parser import GraphQLParser

import boto3

# Create an AppSync client

'''appsyncconnection'''
client = boto3.client('appsync', aws_access_key_id='xxx',aws_secret_access_key='xxx', aws_session_token=aws_session_token, region_name=region)

# Get introspection schema data from AppSync

response = client.get_introspection_schema(apiId='API ID', format='JSON',    includeDirectives=True)

# Parse the introspection schema JSON

response_data = response\['schema'\].read().decode('utf-8')


response_data_json = json.loads(response_data)

#below is my query to **query_to_validate

query_to_validate = '''query MyQuery {
indicatorsConnection(first: 10, groupIds: "somedataofids") {
edges{ node{ id orientation } }
}
}'''

#Parse the query using GraphQLParser

parser = GraphQLParser()

ast = parser.parse(query_to_validate)

#Validate the query against the introspection schema

errors = validate(response_data_json, ast)

if errors:

    print("Query is not valid:")

for error in errors:

    print(error)
else:
    print("Query is valid.")

results is error message

0

There are 0 best solutions below