def with_extensions(): schema_extensions = {"schemaExtension": "schema"} schema = GraphQLSchema(extensions=schema_extensions) assert schema.extensions is schema_extensions assert schema.to_kwargs()["extensions"] is schema_extensions
def preserves_the_order_of_user_provided_types(): a_type = GraphQLObjectType( "A", {"sub": GraphQLField(GraphQLScalarType("ASub"))}) z_type = GraphQLObjectType( "Z", {"sub": GraphQLField(GraphQLScalarType("ZSub"))}) query_type = GraphQLObjectType( "Query", { "a": GraphQLField(a_type), "z": GraphQLField(z_type), "sub": GraphQLField(GraphQLScalarType("QuerySub")), }, ) schema = GraphQLSchema(query_type, types=[z_type, query_type, a_type]) type_names = list(schema.type_map) assert type_names == [ "Z", "ZSub", "Query", "QuerySub", "A", "ASub", "Boolean", "String", "__Schema", "__Type", "__TypeKind", "__Field", "__InputValue", "__EnumValue", "__Directive", "__DirectiveLocation", ] # Also check that this order is stable copy_schema = GraphQLSchema(**schema.to_kwargs()) assert list(copy_schema.type_map) == type_names
def define_sample_schema(): BlogImage = GraphQLObjectType( "Image", { "url": GraphQLField(GraphQLString), "width": GraphQLField(GraphQLInt), "height": GraphQLField(GraphQLInt), }, ) BlogArticle: GraphQLObjectType BlogAuthor = GraphQLObjectType( "Author", lambda: { "id": GraphQLField(GraphQLString), "name": GraphQLField(GraphQLString), "pic": GraphQLField( BlogImage, args={ "width": GraphQLArgument(GraphQLInt), "height": GraphQLArgument(GraphQLInt), }, ), "recentArticle": GraphQLField(BlogArticle), }, ) BlogArticle = GraphQLObjectType( "Article", lambda: { "id": GraphQLField(GraphQLString), "isPublished": GraphQLField(GraphQLBoolean), "author": GraphQLField(BlogAuthor), "title": GraphQLField(GraphQLString), "body": GraphQLField(GraphQLString), }, ) BlogQuery = GraphQLObjectType( "Query", { "article": GraphQLField(BlogArticle, args={"id": GraphQLArgument(GraphQLString)}), "feed": GraphQLField(GraphQLList(BlogArticle)), }, ) BlogMutation = GraphQLObjectType( "Mutation", {"writeArticle": GraphQLField(BlogArticle)}) BlogSubscription = GraphQLObjectType( "Subscription", { "articleSubscribe": GraphQLField(args={"id": GraphQLArgument(GraphQLString)}, type_=BlogArticle) }, ) schema = GraphQLSchema(BlogQuery, BlogMutation, BlogSubscription) kwargs = schema.to_kwargs() types = kwargs.pop("types") assert types == list(schema.type_map.values()) assert kwargs == { "query": BlogQuery, "mutation": BlogMutation, "subscription": BlogSubscription, "directives": None, "ast_node": None, "extensions": None, "extension_ast_nodes": None, "assume_valid": False, } assert print_schema(schema) == dedent(""" type Article { id: String isPublished: Boolean author: Author title: String body: String } type Author { id: String name: String pic(width: Int, height: Int): Image recentArticle: Article } type Image { url: String width: Int height: Int } type Mutation { writeArticle: Article } type Query { article(id: String): Article feed: [Article] } type Subscription { articleSubscribe(id: String): Article } """)
def without_extensions(): schema = GraphQLSchema() assert schema.extensions is None assert schema.to_kwargs()["extensions"] is None