Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL (needs package & maintainer) #1471

Closed
astorije opened this issue Mar 10, 2017 · 13 comments
Closed

GraphQL (needs package & maintainer) #1471

astorije opened this issue Mar 10, 2017 · 13 comments
Labels
help welcome Could use help from community language

Comments

@astorije
Copy link

GraphQL can describe both a type system and a query language, both in a consistent manner.

Example of a type (GitHub code blocks support GraphQL):

type Project {
  name: String
  tagline: String
  contributors: [User]
}

Example of a query:

query HeroForEpisode($ep: Episode!) {
  hero(episode: $ep) {
    name
    ... on Droid {
      primaryFunction
    }
    ... on Human {
      height
    }
  }
}
@schickling
Copy link

Any progress on this?

@nic
Copy link

nic commented Oct 5, 2017

hljs.registerLanguage("graphql",function(e){return{aliases:["gql"],k:{keyword:"query mutation subscription|10 type interface union scalar fragment|10 enum on ...",literal:"true false null"},c:[e.HCM,e.QSM,e.NM,{cN:"type",b:"[^\\w][A-Z][a-z]",e:"\\W",eE:!0},{cN:"literal",b:"[^\\w][A-Z][A-Z]",e:"\\W",eE:!0},{cN:"variable",b:"\\$",e:"\\W",eE:!0},{cN:"keyword",b:"[.]{2}",e:"\\."},{cN:"meta",b:"@",e:"\\W",eE:!0}],i:/([;<']|BEGIN)/}});

@astorije
Copy link
Author

astorije commented Oct 5, 2017

There is also an open PR on this: #1543
@nic, do you want to help them by providing an external review there?

@nodkz
Copy link

nodkz commented Oct 27, 2018

Five cents to @nic's solution:

hljs.registerLanguage("graphql",function(e){return{aliases:["gql"],k:{keyword:"query mutation subscription|10 type input schema directive interface union scalar fragment|10 enum on ...",literal:"true false null"},c:[e.HCM,e.QSM,e.NM,{cN:"type",b:"[^\\w][A-Z][a-z]",e:"\\W",eE:!0},{cN:"literal",b:"[^\\w][A-Z][A-Z]",e:"\\W",eE:!0},{cN:"variable",b:"\\$",e:"\\W",eE:!0},{cN:"keyword",b:"[.]{2}",e:"\\."},{cN:"meta",b:"@",e:"\\W",eE:!0}],i:/([;<']|BEGIN)/}});

Added schema, directive and input keywords.

@morleym
Copy link

morleym commented Dec 7, 2018

I really wish this were implemented because Docusaurus is using Highlight.js and that means all of our GraphQL examples with fenced code blocks do not have nice *highlighting for the requests (queries).

@PulsGarney
Copy link

Dunno how to get the example above to work since I am using highlight.js via npm.

But based on @nodkz 's snippet I eventually kinda unwrap it into the unpacked version and got it running now. I am not quite sure if I had did it right, but at lease it's working now.

Put it here in case anyone needs it. And if I got it wrong somewhere please let me know. 😂

import hljs from 'highlight.js';


hljs.registerLanguage("graphql", function (e) {
  return {
    aliases: ["gql"],
    keywords: {
      keyword: "query mutation subscription|10 type input schema directive interface union scalar fragment|10 enum on ...",
      literal: "true false null"
    },
    contains: [
      e.HASH_COMMENT_MODE,
      e.QUOTE_STRING_MODE,
      e.NUMBER_MODE,
      {
        className: "type",
        begin: "[^\\w][A-Z][a-z]",
        end: "\\W",
        excludeEnd: !0,
      },
      {
        className: "literal",
        begin: "[^\\w][A-Z][A-Z]",
        end: "\\W",
        excludeEnd: !0,
      },
      {
        className: "variable",
        begin: "\\$",
        end: "\\W",
        excludeEnd: !0,
      },
      {
        className: "keyword",
        begin: "[.]{2}",
        end: "\\.",
      },
      {
        className: "meta",
        begin: "@",
        end: "\\W",
        excludeEnd: !0,
      },
    ],
    illegal: /([;<']|BEGIN)/,
  };
});


export default hljs;

And now you can just import hljs from this file to get it going.

@morleym
Copy link

morleym commented Mar 11, 2019

@PulsGarney this works, one thing to point out is that this does not highlight query/mutation names or variable types the same way as GitHub's highlighting.

query Foo ($id: Int!) {
  thing(id: $id){
    id
    name
  }
}

Whereas the result in this highlighting in your snippet looks like this (on the atom-one-light style):

image

I'm guessing the next step has more to do with grammar to highlight, like here: https://github.com/rmosolgo/language-graphql/blob/master/grammars/graphql.json

@joshgoebel joshgoebel changed the title Add support for GraphQL Add grammar for GraphQL Oct 7, 2019
@joshgoebel joshgoebel added help welcome Could use help from community new language labels Oct 7, 2019
@joshgoebel
Copy link
Member

Is anyone willing to package this up nicely into a repository that we could link to or host here inside the highlightjs organization (we'd need a maintainer for it)... This needs a better home than living inside this issue. We're trying to clear out all the super old issues.

@joshgoebel joshgoebel changed the title Add grammar for GraphQL GraphQL (needs package & maintainer) Oct 17, 2019
@stephencweiss
Copy link

stephencweiss commented Dec 1, 2019

@yyyc514 - what is involved in packaging / maintaining? Would love to help as I'm finding myself in need of a good syntax highlighting for graphql and we use highlightjs.

(Said another way - if you have an example you can point me to, I'd love to give it a go)

Cheers!

@joshgoebel
Copy link
Member

Preferably just someone knowledgable with Highlight.js language grammar functionality and also familiar with GraphQL - who could handle issues and fixes that people raised with the grammar. Right now there is the code and PRs above... they'd need to be pulled out into their own repo - and then you'd become the maintainer of said repo.

There aren't any docs for this yet but there are a few sample repos (though I think the specifics may change after we circle back around to packaging). I plan to get to that in the next iteration.

Samples can be found in the highlightjs org:
https://github.com/highlightjs/

@joshgoebel
Copy link
Member

@stephencweiss The 3rd party stuff is pretty ready to go now if you wanted to take a shot at this.

@joshgoebel
Copy link
Member

Closing this issue for lack of activity. Any one who'd like to take what's here and step up and contribute a 3rd party language grammar, that would be awesome. :-)

@dadooda
Copy link

dadooda commented May 2, 2020

Those willing to use the recipe with TypeDoc code fences in Markdown, consider such typedoc.js outline (ES6 syntax):

const hljs = require("highlight.js")

// Register GraphQL highlighting for code fences.
// See https://github.com/highlightjs/highlight.js/issues/1471.
hljs.registerLanguage("graphql", function (e) {
  return {
    aliases: ["gql"],
    keywords: {
      keyword: "query mutation subscription|10 type input schema directive interface union scalar fragment|10 enum on ...",
      literal: "true false null",
    },
     (see code above)
  }
})

// TypeDoc configuration.
module.exports = {
  out: "docs",
   (additional TypeDoc settings)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help welcome Could use help from community language
Projects
None yet
Development

No branches or pull requests

9 participants