GithubHelp home page GithubHelp logo

davisein / graphql-core Goto Github PK

View Code? Open in Web Editor NEW

This project forked from graphql-python/graphql-core-legacy

0.0 2.0 0.0 1.25 MB

GraphQL base implementation for Python

License: MIT License

Shell 0.03% Python 99.97%

graphql-core's Introduction

GraphQL-core

GraphQL for Python.

This library is a port of graphql-js to Python and currently is up-to-date with release 0.6.0.

PyPI version Build Status Coverage Status Public Slack Discussion

See more complete documentation at http://graphql.org/ and http://graphql.org/docs/api-reference-graphql/.

For questions, ask Stack Overflow.

Getting Started

An overview of the GraphQL language is available in the README for the Specification for GraphQL.

The overview describes a simple set of GraphQL examples that exist as tests in this repository. A good way to get started is to walk through that README and the corresponding tests in parallel.

Using graphql-core

Install from pip:

pip install graphql-core

GraphQL.js provides two important capabilities: building a type schema, and serving queries against that type schema.

First, build a GraphQL type schema which maps to your code base.

from graphql import (
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLField,
    GraphQLString
)

schema = GraphQLSchema(
  query= GraphQLObjectType(
    name='RootQueryType',
    fields={
      'hello': GraphQLField(
        type= GraphQLString,
        resolver=lambda *_: 'world'
      )
    }
  )
)

This defines a simple schema with one type and one field, that resolves to a fixed value. The resolve function can return a value, a promise, or an array of promises. A more complex example is included in the top level tests directory.

Then, serve the result of a query against that type schema.

query = '{ hello }'

result = graphql(schema, query)

# Prints
# {'hello': 'world'} (as OrderedDict)

print result.data

This runs a query fetching the one field defined. The graphql function will first ensure the query is syntactically and semantically valid before executing it, reporting errors otherwise.

query = '{ boyhowdy }'

result = graphql(schema, query)

# Prints
# [GraphQLError('Cannot query field "boyhowdy" on type "RootQueryType".',)]

print result.errors

Executors

The graphql query is executed, by default, synchronously (using SyncExecutor). However the following executors are available if we want to resolve our fields in parallel:

  • graphql.execution.executors.asyncio.AsyncioExecutor: This executor executes the resolvers in the Python asyncio event loop.
  • graphql.execution.executors.gevent.GeventExecutor: This executor executes the resolvers in the Gevent event loop.
  • graphql.execution.executors.process.ProcessExecutor: This executor executes each resolver as a process.
  • graphql.execution.executors.thread.ThreadExecutor: This executor executes each resolver in a Thread.
  • graphql.execution.executors.sync.SyncExecutor: This executor executes each resolver synchronusly (default).

Usage

You can specify the executor to use via the executor keyword argument in the grapqhl.execution.execute function.

from graphql.execution.execute import execute

execute(schema, ast, executor=SyncExecutor())

Main Contributors

License

MIT License

graphql-core's People

Contributors

akira-dev avatar alecaivazis avatar audiolion avatar bcb avatar caselit avatar cpennington avatar dittos avatar ekampf avatar faassen avatar femesq avatar gabriel-laet avatar globegitter avatar jhgg avatar mcamac avatar mwilliamson avatar obi1kenobi avatar pmlandwehr avatar rapthead avatar sjhewitt avatar syrusakbary avatar tgriesser avatar valdergallo avatar woodb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.