Skip to content

Simpler GraphQL syntax? #4654

Closed
Closed

Description

@fregante

Thinking out loud…

What if we went from:

repository() {
	ref(qualifiedName: "${branch}") {
		target {
			... on Commit {
				history(first: 1, path: "${filePath}") {
					nodes {
						oid
						commitDate
					}
				}
			}
		}
	}
}

To:

repository()
ref(qualifiedName: "${branch}")
target
... on Commit
history(first: 1, path: "${filePath}")
nodes
oid commitDate

where every line break is a { and every missing } is appended.

Most of our queries are allowed with this syntax. Others can keep using the native syntax.

It would be its own helper like:

api.v4(ql`
	  repository()
	  ref(qualifiedName: "${branch}")
	  target
	  ... on Commit
	  history(first: 1, path: "${filePath}")
	  nodes
	  oid commitDate
`)

and multiple queries can be concatenated as strings too:

api.v4(ql`
	  repository()
	  ref(qualifiedName: "${branch}")
	  target
	  ... on Commit
	  history(first: 1, path: "${filePath}")
	  nodes
	  oid commitDate
` + ql`
	  query2: repository()
	  name
`)

Thoughts? Worth it? 😂

Activity

  1. added
    meta
    on Aug 11, 2021
  2. yakov116 commented on Aug 11, 2021

    @yakov116
    Member

    I think that kills the beauty of GraphQL. What I like it most is the way the call is made, that is what the response looks like.

    It may take up more "lines" but I think the other way is clearer. JMHO

  3. fregante commented on Aug 11, 2021

    @fregante
    MemberAuthor

    Is that affected though? The only problems I see are:

    • the query is less copypastable (it already isn't fully so)
    • it's not immediately understandable how it translates into GQL

    that is what the response looks like.

    Almost. The GQL "object" is imprecise:

    • nodes {} is an array
    • ...on Commit {} doesn't appear

    As a compromise I thought we could just automatically close any unmatched {, and that would already make it a little shorter:

    repository() {
    	ref(qualifiedName: "${branch}") {
    		target {
    			... on Commit {
    				history(first: 1, path: "${filePath}") {
    					nodes {
    						oid
    						commitDate

    It'd be cool to make it indentation-based and drop the { as well, but it's too easy to mix spaces and tabs since the linter does not complain.

  4. fregante commented on Aug 11, 2021

    @fregante
    MemberAuthor

    Is that affected though? The only problems I see are:

    • the query is less copypastable (it already isn't fully so)
    • it's not immediately understandable how it translates into GQL

    For this we could set up:

    • console.debug for every call in api.v4 when installed locally
    • a micro service/REPL that compiles these queries into GQL so you can play with them online
  5. yakov116 commented on Aug 11, 2021

    @yakov116
    Member
    • console.debug for every call in api.v4 when installed locally

    Big 👍

  6. fregante commented on Aug 11, 2021

    @fregante
    MemberAuthor

    We can already do that, including the query {} part. PR welcome

  7. sindresorhus commented on Aug 16, 2021

    @sindresorhus
    Member

    I find the original syntax more readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    metaunder discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions