Skip to content

Generate schema with directive at field #90

Closed
@hspedro

Description

@hspedro

Feature requests

Hi all, I'm trying to generate a schema that has a directive at a field:

directive @hasPermissions(permissions: [String]) on OBJECT | FIELD_DEFINITION

type MyType {
  field1: Boolean @hasPermissions(permissions: ['permission1', 'permission2'])
}

type Query {
  myType: MyType
}

However, I did not found a way to generate this using the library. For instance, I tried defining the directives attribute from GraphQLField.ast_node but print_schema did not generated field with the directive:

from graphql.language import (
    FieldDefinitionNode, DirectiveLocation, DirectiveNode, ArgumentNode
)
from graphql.type import (
    GraphQLArgument,
    GraphQLBoolean,
    GraphQLDirective,
    GraphQLField,
    GraphQLList,
    GraphQLObjectType,
    GraphQLSchema,
    GraphQLString,
)
from graphql.utilities import print_schema

if __name__ == "__main__":
    MyType = GraphQLObjectType(
        "MyType",
        lambda: {
            "field1": GraphQLField(
                GraphQLBoolean,
                ast_node=FieldDefinitionNode(
                    directives=[DirectiveNode(
                        name="hasPermissions",
                        arguments=[
                            ArgumentNode(
                                name="permissions",
                                value=["permission1", "permission2"]
                            )
                        ]
                    )]
                )
            ),
        },
    )

    MyTypeQuery = GraphQLObjectType(
        "Query",
        {
            "myType": GraphQLField(MyType),
        },
    )
    schema = GraphQLSchema(
        directives=[GraphQLDirective(
            name="hasPermissions",
            locations=[
                DirectiveLocation.OBJECT,
                DirectiveLocation.FIELD_DEFINITION
            ],
            args={
                "permissions": GraphQLArgument(GraphQLList(GraphQLString)),
            },
        )],
        query=MyTypeQuery,
    )
    print(print_schema(schema))

Output:

directive @hasPermissions(permissions: [String]) on OBJECT | FIELD_DEFINITION

type Query {
  myType: MyType
}

type MyType {
  field1: Boolean
}

Is there any way to generate a schema with directive at the field declaration? If not, I can contribute on that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions