def skips_all_description_fields(): has_descriptions = re.compile(r"\bdescription\b").search assert has_descriptions(get_introspection_query()) assert has_descriptions(get_introspection_query(descriptions=True)) assert not has_descriptions( get_introspection_query(descriptions=False))
def includes_is_repeatable_field_on_directives(): has_repeatability = re.compile(r"\bisRepeatable\b").search assert not has_repeatability(get_introspection_query()) assert has_repeatability( get_introspection_query(directive_is_repeatable=True)) assert not has_repeatability( get_introspection_query(directive_is_repeatable=False))
def includes_specified_by_url_field(): all_specified_by_urls = re.compile(r"\bspecifiedByUrl\b").findall assert not all_specified_by_urls(get_introspection_query()) assert not all_specified_by_urls( get_introspection_query(specified_by_url=False)) assert (len( all_specified_by_urls( get_introspection_query(specified_by_url=True))) == 1)
def introspection(): class Lazy: def __init__(self, text: str): self.text = text self.evaluated = False def __str__(self) -> str: self.evaluated = True return self.text description = Lazy("a lazy description") deprecation_reason = Lazy("a lazy reason") with registered(Lazy): field = GraphQLField( GraphQLString, description=cast(str, description), deprecation_reason=cast(str, deprecation_reason), ) schema = GraphQLSchema(GraphQLObjectType("Query", {"lazyField": field})) query = get_introspection_query(descriptions=True) assert not description.evaluated assert not deprecation_reason.evaluated result = graphql_sync(schema, query) assert description.evaluated assert deprecation_reason.evaluated assert result.data introspected_query = result.data["__schema"]["types"][0] assert introspected_query["name"] == "Query" introspected_field = introspected_query["fields"][0] assert introspected_field["name"] == "lazyField" assert introspected_field["description"] == "a lazy description" assert introspected_field["deprecationReason"] == "a lazy reason"
async def test_should_not_trace_introspection_async_queries(mocker): mocker.patch( "strawberry.extensions.tracing.apollo.time.perf_counter_ns", return_value=0 ) @strawberry.type class Person: name: str = "Jess" @strawberry.type class Query: @strawberry.field async def person(self, info) -> Person: return Person() schema = strawberry.Schema(query=Query, extensions=[ApolloTracingExtension]) result = await schema.execute(get_introspection_query()) assert not result.errors assert result.extensions == { "tracing": { "version": 1, "startTime": "2012-01-14T12:00:01.000000Z", "endTime": "2012-01-14T12:00:01.000000Z", "duration": 0, "execution": {"resolvers": []}, "validation": {"startOffset": 0, "duration": 0}, "parsing": {"startOffset": 0, "duration": 0}, } }
def includes_description_field_on_schema(): all_descriptions = re.compile(r"\bdescription\b").findall assert len(all_descriptions(get_introspection_query())) == 5 assert (len( all_descriptions( get_introspection_query(schema_description=False))) == 5) assert (len( all_descriptions( get_introspection_query(schema_description=True))) == 6) assert not all_descriptions( get_introspection_query(descriptions=False, schema_description=True))
def executes_introspection_query_without_calling_global_field_resolver(): query_root = GraphQLObjectType( 'QueryRoot', {'onlyField': GraphQLField(GraphQLString)}) schema = GraphQLSchema(query_root) source = get_introspection_query() called_for_fields = set() def field_resolver(value, info): called_for_fields.add( f'{info.parent_type.name}::{info.field_name}') return value graphql_sync(schema, source, field_resolver=field_resolver) assert not called_for_fields
def test_get_introspection_query_ast(option): introspection_query = get_introspection_query( descriptions=option, specified_by_url=option, directive_is_repeatable=option, schema_description=option, ) dsl_introspection_query = get_introspection_query_ast( descriptions=option, specified_by_url=option, directive_is_repeatable=option, schema_description=option, ) assert print_ast(gql(introspection_query)) == print_ast(dsl_introspection_query)
def test_validate_introspection_query(benchmark, big_schema_sdl): # noqa: F811 schema = build_schema(big_schema_sdl, assume_valid=True) query = parse(get_introspection_query()) result = benchmark(lambda: validate(schema, query)) assert result == []
def __init__(self, **options): query = get_introspection_query(**options) validation_errors = validate(dummy_schema, parse(query)) assert validation_errors == [] self.query = query
def executes_an_introspection_query(): EmptySchema = GraphQLSchema( GraphQLObjectType('QueryRoot', {'onlyField': GraphQLField(GraphQLString)})) query = get_introspection_query(descriptions=False) result = graphql_sync(EmptySchema, query) assert result.errors is None assert result.data == { '__schema': { 'mutationType': None, 'subscriptionType': None, 'queryType': { 'name': 'QueryRoot' }, 'types': [{ 'kind': 'OBJECT', 'name': 'QueryRoot', 'fields': [{ 'name': 'onlyField', 'args': [], 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None, }, 'isDeprecated': False, 'deprecationReason': None }], 'inputFields': None, 'interfaces': [], 'enumValues': None, 'possibleTypes': None }, { 'kind': 'SCALAR', 'name': 'String', 'fields': None, 'inputFields': None, 'interfaces': None, 'enumValues': None, 'possibleTypes': None }, { 'kind': 'OBJECT', 'name': '__Schema', 'fields': [{ 'name': 'types', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } } } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'queryType', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'mutationType', 'args': [], 'type': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'subscriptionType', 'args': [], 'type': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'directives', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Directive', 'ofType': None } } } }, 'isDeprecated': False, 'deprecationReason': None }], 'inputFields': None, 'interfaces': [], 'enumValues': None, 'possibleTypes': None }, { 'kind': 'OBJECT', 'name': '__Type', 'fields': [{ 'name': 'kind', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'ENUM', 'name': '__TypeKind', 'ofType': None } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'name', 'args': [], 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'description', 'args': [], 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'fields', 'args': [{ 'name': 'includeDeprecated', 'type': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None }, 'defaultValue': 'false' }], 'type': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Field', 'ofType': None } } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'interfaces', 'args': [], 'type': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'possibleTypes', 'args': [], 'type': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'enumValues', 'args': [{ 'name': 'includeDeprecated', 'type': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None }, 'defaultValue': 'false' }], 'type': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__EnumValue', 'ofType': None } } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'inputFields', 'args': [], 'type': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__InputValue', 'ofType': None } } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'ofType', 'args': [], 'type': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }], 'inputFields': None, 'interfaces': [], 'enumValues': None, 'possibleTypes': None }, { 'kind': 'ENUM', 'name': '__TypeKind', 'fields': None, 'inputFields': None, 'interfaces': None, 'enumValues': [{ 'name': 'SCALAR', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'OBJECT', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'INTERFACE', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'UNION', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'ENUM', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'INPUT_OBJECT', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'LIST', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'NON_NULL', 'isDeprecated': False, 'deprecationReason': None }], 'possibleTypes': None }, { 'kind': 'SCALAR', 'name': 'Boolean', 'fields': None, 'inputFields': None, 'interfaces': None, 'enumValues': None, 'possibleTypes': None }, { 'kind': 'OBJECT', 'name': '__Field', 'fields': [{ 'name': 'name', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'description', 'args': [], 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'args', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__InputValue', 'ofType': None } } } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'type', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'isDeprecated', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'deprecationReason', 'args': [], 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }], 'inputFields': None, 'interfaces': [], 'enumValues': None, 'possibleTypes': None }, { 'kind': 'OBJECT', 'name': '__InputValue', 'fields': [{ 'name': 'name', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'description', 'args': [], 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'type', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'defaultValue', 'args': [], 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }], 'inputFields': None, 'interfaces': [], 'enumValues': None, 'possibleTypes': None }, { 'kind': 'OBJECT', 'name': '__EnumValue', 'fields': [{ 'name': 'name', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'description', 'args': [], 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'isDeprecated', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'deprecationReason', 'args': [], 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }], 'inputFields': None, 'interfaces': [], 'enumValues': None, 'possibleTypes': None }, { 'kind': 'OBJECT', 'name': '__Directive', 'fields': [{ 'name': 'name', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'description', 'args': [], 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'locations', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'ENUM', 'name': '__DirectiveLocation', 'ofType': None } } } }, 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'args', 'args': [], 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__InputValue', 'ofType': None } } } }, 'isDeprecated': False, 'deprecationReason': None }], 'inputFields': None, 'interfaces': [], 'enumValues': None, 'possibleTypes': None }, { 'kind': 'ENUM', 'name': '__DirectiveLocation', 'fields': None, 'inputFields': None, 'interfaces': None, 'enumValues': [{ 'name': 'QUERY', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'MUTATION', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'SUBSCRIPTION', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'FIELD', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'FRAGMENT_DEFINITION', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'FRAGMENT_SPREAD', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'INLINE_FRAGMENT', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'VARIABLE_DEFINITION', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'SCHEMA', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'SCALAR', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'OBJECT', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'FIELD_DEFINITION', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'ARGUMENT_DEFINITION', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'INTERFACE', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'UNION', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'ENUM', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'ENUM_VALUE', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'INPUT_OBJECT', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'INPUT_FIELD_DEFINITION', 'isDeprecated': False, 'deprecationReason': None }], 'possibleTypes': None }], 'directives': [{ 'name': 'include', 'locations': ['FIELD', 'FRAGMENT_SPREAD', 'INLINE_FRAGMENT'], 'args': [{ 'defaultValue': None, 'name': 'if', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } } }] }, { 'name': 'skip', 'locations': ['FIELD', 'FRAGMENT_SPREAD', 'INLINE_FRAGMENT'], 'args': [{ 'defaultValue': None, 'name': 'if', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } } }] }, { 'name': 'deprecated', 'locations': ['FIELD_DEFINITION', 'ENUM_VALUE'], 'args': [{ 'defaultValue': '"No longer supported"', 'name': 'reason', 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }] }] } }
def test_execute_introspection_query(benchmark, big_schema_sdl): # noqa: F811 schema = build_schema(big_schema_sdl, assume_valid=True) document = parse(get_introspection_query()) result = benchmark(lambda: execute_sync(schema=schema, document=document)) assert result.errors is None
def __init__(self, **options): self.query = get_introspection_query(**options)