GraphQL
Relay
GitHub also announced GraphQL support for its open API

Because GraphQL support needs to be changed on the server side, I choose to write an intermediate layer with Node.js based on LeanCloud’s data service, run on the cloud engine, translate GraphQL queries into LeanCloud SDK calls. GraphQL support for clients.

I also looked at GraphQL support for other languages and frameworks, all of which require a lot of development or configuration work by developers. This is because no association between data is recorded in either MySQL or MongoDB (Id and ObjectId don’t record the tables or collections they point to, although MySQL foreign keys do, but unfortunately there aren’t many users). And even if you define relationships between data, you still need to define permissions — which users have access to which data.

LeanCloud also has a permission control mechanism based on sessionToken and ACL. Therefore, it is perfectly possible to manage data between LeanCloud’s data services and follow existing ACLs to automatically implement GraphQL support.

One such project is leanCloud/leanCloud-GraphQL. You just deploy it to the cloud engine, and without changing a line of code, you can query all your data on leanCloud with GraphQL.

In contrast to RESTful and SQL, GraphQL defines strict types for data, so you can use a flexible language to combine data through relationships to get the data you want as you see it. Thanks to GraphQL’s type system, you can also get accurate error prompts and completion suggestions in the debug tool (graphql.leanapp.cn).

Select * from Todo; select * from Todo; select * from Todo;


query {
  Todo(ascending: priority, limit: 2) {
    title, priority, owner {
      username
    }
  }
}
Copy the code

Results:

{
  Todo: [{
    title: "紧急 Bug 修复",
    priority: 0,
    owner: {
      username: "someone"
    }
  }, {
    title: "打电话给 Peter",
    priority: 5,
    owner: {
      username: "someone"
    }
  }]
}
Copy the code

Leancloud-graphql already implements most of the query parameters and query criteria in leanCloud. You can combine these criteria as you like. For example, we can query data with priority greater than 5 and the content attribute:


query {
  Todo(exists: {content: true}, greaterThan: {priority: 5}) {
    title, content, priority
  }
}
Copy the code

One of the best things about GraphQL is its support for relational queries. You can expand Relation and Pointer anywhere you want without having to use LeanCloud RESTful apis to expand only one layer. For example, we query Todo (Relation) in all TodoFolder and expand owner (Pointer) :

query {
  TodoFolder {
    name,
    containedTodos {
      title, owner {
        username, email
      }
    }
  }
}
Copy the code

Results (partially omitted) :

{TodoFolder: [{name: "work ", containedTodos: [{title:" urgent Bug fix ", owner: {username: "someone", email: "[email protected]" } }, // ... ] }, // ... ] }Copy the code

You can also attach query parameters or conditions to relational queries. For example, we query the Todo with the highest priority among all todoFolders:

query {
  TodoFolder {
    name, containedTodos(limit: 1, ascending: priority) {
      title, priority
    }
  }
}
Copy the code

Results:

{TodoFolder: [{name: "work ", containedTodos: [{title:" urgent Bug fix ", priority: 0}]}, name: "Shopping list ", containedTodos: [{title:" urgent Bug fix ", priority: 0}]} [{title: "I ", priority: 10}]}, {name: "someone", containedTodos: [{title:" I ", priority: 0}]}]Copy the code

When implementing a one-to-many relationship, we often store a Pointer to a “one” over a “many”. For example, a Todo will have a Pointer called owner pointing to the user table. In this case, leanCloud-GraphQL automatically adds a property called ownerOfTodo to the user table to represent the inverse relationship. You can expand the inverse relationship like a Relation, for example, we query each user’s Todo and expand title:

query {
  _User {
    username, ownerOfTodo {
      title
    }
  }
}
Copy the code

Results:

{_User: [{username: "someone", ownerOfTodo: [{title: "urgent Bug fix "}, {title:" call Peter"}, {title: "pay the credit card bill "}, {title: "someone", ownerOfTodo: [{title:" urgent Bug fix "}, {title: "call Peter"}, {title:" pay the credit card bill "}, {title: "Buy yogurt "}]}]Copy the code

That’s it for a brief introduction to leanCloud/leanCloud-GraphQL. You can find more information about how to use it and how to use it on the GitHub homepage of the project, which is also open source.