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

Support graphql imports #722

Closed
dobesv opened this issue Jun 1, 2022 · 6 comments · Fixed by #726
Closed

Support graphql imports #722

dobesv opened this issue Jun 1, 2022 · 6 comments · Fixed by #726
Labels

Comments

@dobesv
Copy link
Contributor

dobesv commented Jun 1, 2022

Feature request description

GraphQL is a popular technology, which uses files with the .graphql extension to define schemas and operations. In many environments, these files can import other files (including from packages) use a syntax like:

#import 'path/to/other.graphql`
#import './whatever.graphql`

The given path is passed directly to node's require mechanism.

Example parser implementation:

const requirePackageName = require('require-package-name');

const graphqlParser = (filePath, deps, rootDir) => {
  const foundDeps = [];
  const content = readFileSync(filePath, {encoding: 'utf8'});
  const lines = content.split(/\r\n|\r|\n/);
  for (const line of lines) {
    if (line[0] === '#' && line.slice(1).split(' ')[0] === 'import') {
      const importFileExpr = line.slice(1).split(' ')[1];
      if (importFileExpr) {
        // Need to support single and double quotes, so use JSON5
        const importFile = JSON5.parse(importFileExpr) as any;
        if (importFile && typeof importFile === 'string') {
          foundDeps.push(importFile);
        }
      }
    }
    // Imports must be in a comment block at the start of the file
    if (line.length !== 0 && line[0] !== '#') {
      break;
    }
  }
  return foundDeps.map(p => requirePackageName(p)).filter(Boolean);;
};
@dobesv dobesv added the feature label Jun 1, 2022
@rumpl
Copy link
Member

rumpl commented Jun 1, 2022

I didn't know graphql can do that, is there a parser somewhere that we can use instead of splitting strings?

@dobesv
Copy link
Contributor Author

dobesv commented Jun 1, 2022

I got the implementation from here:

https://github.com/apollographql/graphql-tag/blob/main/loader.js#L29

If you try to use that parser, it's going to give you JavaScript which you can then parse. I had attempted to go that route at first but the textual parsing seems more efficient and doesn't add a lot of dependencies.

@rumpl
Copy link
Member

rumpl commented Jun 1, 2022

Fair enough, if apollo does it that way then no need for us to get fancy about it :)

@dobesv
Copy link
Contributor Author

dobesv commented Jun 1, 2022

The code above doesn't quite work, though ... still working on it.

@dobesv
Copy link
Contributor Author

dobesv commented Jun 3, 2022

Would you accept a PR for this feature? Or should I just keep this local to our project?

@rumpl
Copy link
Member

rumpl commented Jun 3, 2022

Oh I would definitely accept a PR for this! :)

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants