GraphQL_API

Categories

Component ID

2679156

Component name

GraphQL_API

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Component created

Component changed

Component body

GraphQL Service Endpoint for Drupal 8 entities.

Hello, this is my first Community contribution, as well as my first Drupal 8 project. I still have much to learn.

I am developing this project simultaneously for Drupal 8 and Drupal 6 in hopes of keeping a Drupal back-end while moving my websites' clients to React and React Native.

This is just a sandbox project, and there IS another GraphQL project out there (https://www.drupal.org/project/graphql), that may be maintained a bit more professionally, although mine seems to be a little further along from what I've seen, so I thought I'd share it with the community.

For those who don't know, GraphQL is Facebook's successor to REST API's, which eliminates the need for versioning your feeds.

See https://facebook.github.io/react/blog/2015/05/01/graphql-introduction.html for more information.

For now, this project takes a YAML file, compares with your user and node content types, and creates a Queryable GraphQL service from the intersection. It also allows you to set your own logic for types, in case you need fields to be computed by code instead of queried from the CMS.

Endpoints provided include:

  • /graphql - graphQL service
  • /graphql_shorthand - a Shorthand Description of your GraphQL schema
  • /graphql_schema - the schema.json file needed in React-Relay projects
  • /graphiql - an in-browser GraphQL IDE (https://github.com/graphql/graphiql)

Near-future plans to the module include:

  • automatic generation of "Connection" and "Edge" reserved types used by Relay (https://facebook.github.io/relay/graphql/connections.htm)
  • access control
  • expansion to other entity types
  • an interface for editing the YAML file from the front end (I assume Drupal allows it)
  • numerous performance and stability enhancements.

Sample config/install/graphql.config.yml file:

node:
  heroclass:
    query_name: 'heroClass'
    type_name: 'HeroClass'
    plural_name: 'heroClasses'
    connections: 'DIRECT'
    fields:
      title: 'name'
      body: 'description'
      field_attacks_per_turn: 'attacksPerTurn'
      field_maximum_a_atk: 'maxAreaAttack'
      field_maximum_a_def: 'maxAreaDefense'
      field_maximum_a_rgn: 'maxAreaRegen'
      field_maximum_atk: 'maxAttack'
      field_maximum_def: 'maxDefense'
      field_maximum_hp: 'maxHp'
user:
  user:
    query_name: 'user'
    type_name: 'User'
    plural_name: 'users'
    connections: 'DIRECT'
    resolver_class: 'DrupalgraphqlCustomUserResolver'
    fields:
      name: 'name'

Psuedo code of the above yaml file:

entity type:
  entity name:
    query_name: 'thisNameIsUsedForSingularQueriesACamelCaseSingularNameIsSuggested'
    type_name: 'ThisNameIsYourTypeNameUnderScoresAreAllowedButUncommon'
    plural_name: 'thisNameIsUsedToGeneratePluralQueryFields'
    connections: 'DIRECT' # leave this as DIRECT for now.  Setting to GRAPH or BOTH will generate the Relay Cursor connections, which are not fully implemented yet.
    resolver_class: overrides default handling of your class to allow your own implementation/mutations, see included user example.
     fields:
      drupal_system_field_name: 'whatGraphQLShouldCallTheFieldAsAMemberOfThisType'

Shorthand output of the above example:

interface Node {
	id: ID!
}

type HeroClass : Node {
	id: ID!
	name: String!
	description: [String]
	attacksPerTurn: Int!
	maxAreaAttack: Int!
	maxAreaDefense: Int!
	maxAreaRegen: Int!
	maxAttack: Int!
	maxDefense: Int!
	maxHp: Int!
}
type User : Node {
	id: ID!
	name: String!
}
type Viewer {
	heroClasses(ids:[ID]): [HeroClass]
	users(ids: [ID],name: String): [User]
}
type Query {
	heroClass(id:ID!): HeroClass
	user(id:ID!): User
	viewer : Viewer
	node(id:ID!): Node
}
type Mutation {
	login(userName: String!,password: String!): User
	logout: User
}