1

I am using graphql-ruby in my rails application. Currently, my app's directory structure looks like this.

app 
 - controllers 
  - application_controller.rb
  - graphql_controller.rb
  - admin
    - application_controller.rb
    - graphql_controller.rb
- graphql
  - types
  - mutations
  - graphql_schema.rb

I am trying to make a simple admin page but I'm not sure how I should hanlde namespaces with graphql-ruby.

Should I make Admin directory under graphql as well and make types and mutations under it for the data I want to use on the admin page??

Also, should I make another endpoint for Admin like the code below??

Rails.application.routes.draw do
 namespace :admin do
   post :graphql, to: 'graphql#execute'
 end
 post :graphql, to: 'graphql#execute'
end

Can you possibly give me the link of a project that does what I am trying to do with graphql-ruby??? That would be a tremendous help.

K-Sato
  • 400
  • 7
  • 26

1 Answers1

1

From https://graphql.org/

GraphQL APIs are organized in terms of types and fields, not endpoints. Access the full capabilities of your data from a single endpoint.

Hence, creating two endpoints as you have suggested would go against that principle. You probably shouldn't do it, but most importantly, there's no need to.

Suppose you have a type ProductType with a couple of fields. You can use that same type to both query/display the product data in your website and edit it with a mutation in the admin page. Granted, you may have to deal with authorizing some specific queries and mutations, but it shouldn't be any harder than dealing with authorization in REST.

See more about GraphQL authorization in Ruby.

Vitor
  • 46
  • 2
  • Thank you for your advice and the links! – K-Sato Apr 01 '19 at 00:23
  • There may well be cases where an app has several distinct types of actors (usually 'visitor', 'registered user', 'admin', etc.) and managing what each of the actors can query for some `ProductType` in one file may get messy. It may be cleaner to expose types intended for different actors and just respond with 403 if there's a mismatch, YMMW. – Epigene May 17 '22 at 09:50