Skip to content

Instantly share code, notes, and snippets.

@tricoder42
Created September 8, 2017 09:38
Show Gist options
  • Save tricoder42/baa447b96503cbf4c416077a955d7cc5 to your computer and use it in GitHub Desktop.
Save tricoder42/baa447b96503cbf4c416077a955d7cc5 to your computer and use it in GitHub Desktop.
GraphQL in Python - Concept
class ProjectType(graphql.DjangoModel):
class Meta:
model = Project
class ProjectSchema(graphql.DetailQuery,
graphql.ListQuery,
graphql.CreateMutation,
graphql.UpdateMutation,
graphql.DeleteMutation,
graphql.Schema):
"""
This will create following schema:
{
query {
project(id: ID): ProjectType,
projects: [ProjectType]
public_projects: [ProjectType]
}
mutation {
createProject(id: ID, data: ProjectType): ProjectType,
updateProject(id: ID, data: ProjectType): ProjectType,
deleteProject(id: ID, data: ProjectType): ProjectType,
starProject(id: ID, star: Boolean): ProjectType,
}
}
"""
object_type = ProjectType
def get_queryset(self, request):
return self.get_queryset().filter(user=request.user)
# Override methods from generic queries/mutations
def detail(self, request, args, query):
pass
# Custom queries
@graphql.query()
def public_projects(self, request, args, query):
pass
# Custom mutation
@graphql.mutation(
args=dict(
id=graphql.ID(),
star=graphql.Boolean()
)
)
def star_project(self, request, args, query):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment