graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

Question about conception of graphql model query/mutation/subscriptions

Open cedrelek opened this issue 9 years ago • 3 comments

Hi, I am trying to play with graphql and to experiment a maybe wierd way of writing my graphql model. The query model is the same as the mutation model, except that mutation types have a 'mutation' node. This gives these kinds of request :

queries

# Request to get all bookmarks of the current viewer. (nothing special)
{
  viewer {
     allBookmarks {
        name
        url
     }
  }
}

or

# request to get the named bookmark of the current viewer. (nothing special)
{
  viewer {
    bookmark(name:"github-graphql-js") {
       name
       url
    }
  }
}

mutations

# add a new bookmark to the current viewer.
mutation {
  viewer {
    mutation {
      addBookmark(name:"github-graphql-js", url:"https://github.com/graphql/graphql-js/")
    }
  }
}

or

# Update a named bookmark of the current user.
mutation {
  viewer {
    bookmark(name:"github-graphql-js") {
      mutation {
        update(url:"https://another-url-to-graphql-js.com/")
      }
    }
  }
}

or

# delete all bookmarks
mutation {
  viewer {
    allBookmarks {
      mutation {
         delete
      }
    }
  }
}

This kind of model is easy to read and easy to manage. I also easly apply some authorizations. If the current viewer does not have the 'viewer[cedrelek]/bookmark/update' authorization, the request will fail at the request validation step. If the bookmark named "github-graphql-js" does not exists the request will fail before hitting the update node. So in the update node resolver, I just have to call a simple bookmarkService.update.

This example is simple, but with a bigger model, it works as well as this simple example. I am just experimenting this way of writing my graphql model so do you have any criticism about this conception ? Or if there some important ideas I missed in graphql that make this conception totally wrong ?

Subscriptions

Because the subscription resolver must provide an AsyncIterator, I can't write query like I do for mutation. A subscription must be defined in the root type. So I can't write this :

subscription {
  viewer {
    application(name:"big-computing-process") {
       name
       subcription {
          logs {
            level
            logMessage
          }
       }
    }
  }
}
# responses would be a stream of :
{
   "viewer" : {
     "application" : {
       "name" : "big-computing-process",
       "subscription" : {
          "logs": {
             "level":"INFO",
             "logMessage": "Initialization done."
          }
       }
     }
   }
}

The the subscribe():AsyncIterator resolver would be called when hitting the 'logs' resolver and the viewer[cedrelek]/application[big-computing-process]/logs authorization will be easly applied there. That would be nice (if the 'viewer' and 'application' don't have to be resolved each time a log is received).

But .... this is very complicated when it is about multiple subscriptions :

subscription {
  viewer {
    allApplications {       #   <== This is an Array
      name
      subscription {       
        logs {                     # <== Because of the array, multiple subscribe() call.....
          level
          logMessage
        }
      }
    }
  }
}

I think multiple subscriptions seems impossible when it is about to think of the responses....

So I just wanted to have some advises about modeling subscription. I would like to not have a list of hundred subscription entry points at the root level.

Could we imagine some intermediate solution to have some kind of "namespaces" ?

subscription {
  applicationNamespace {  # <== Nothing more than just a namespace. basically a very simple resolver.
    logs(appName:"big-computing-process") {  # <== subscribe() resolver.
      level
      logMessage
      timestamp
    }
  }
}

Thank you to have read me until there. I'm eager to read your comments. Cédric

cedrelek avatar Jul 17 '17 18:07 cedrelek

That's an interesting approach. What would it mean to execute something like:

query {
  viewer {
    allBookmarks {
      mutation {
         delete
      }
    }
  }
}

robzhu avatar Jul 21 '17 14:07 robzhu

You are not able to write this, because, you are in a query mode. You would have to write something like :

mutation {
  viewer {
    allBookmarks {
      mutation {
        delete
      }
    }
  }
}

Which simply means that all bookmarks will be deleted. And that mean that the graphql type Bookmark does not have the 'mutation' property in query mode.

This means that in my graphql schema, query AST and mutation AST are not exactly the same. In the mutation AST there are the same types but in 'Mutable' form. Here is the corresponding schema for this example :

schema {
  query:Root
  mutation:MutableRoot
}

type Root {
  viewer:Viewer
}
type MutableRoot {
  viewer:MutableViewer
}
type Viewer {
   allBookmarks:[Bookmark]
   bookmark(name:String):Bookmark
}

type MutableViewer {
  allBookmarks:[MutableBookmark]
  bookmark(name:String):MutableBookmark
  mutation:ViewerMutations
}

type ViewerMutations {
  addBookmark(name:String, url:String):Bookmark
}

type Bookmark {
  name:String
  url:String
}

type MutableBookmark {
  name:String
  url:String
  mutation:BookmarkMutations
}

type BookmarkMutations {
  delete:Boolean
  update(newUrl:String):Bookmark
}

In fact, I have written a JS lib which does that with less effort (I will make it public soon). So, maybe you got a better understanding of my questions about : Am I doing right with this kind of schema ? Do I forget an important thing about graphql that make this schema wrong ? And how about writing subscription in the same way ?

cedrelek avatar Jul 22 '17 11:07 cedrelek

Note that the execution of mutations and subscriptions is different from queries - while technically any field could have a side effect (cause some mutation to underlying systems), only the top level field is executed serially.

It is considered best practice to contain any side-effectful field in the top level Mutation root type only.

leebyron avatar Dec 07 '17 07:12 leebyron