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 definitions #2

Closed
mrasoahaingo opened this Issue Feb 21, 2017 · 1 comment

Comments

Projects
None yet
3 participants
@mrasoahaingo
Copy link

mrasoahaingo commented Feb 21, 2017

I don't know if someone faced the same issue as mine:

 ● Test suite failed to run

    TypeError: Cannot read property 'filter' of undefined

      at Object.parser (node_modules/react-apollo/lib/parser.js:12:41)
      at graphql (node_modules/react-apollo/lib/graphql.js:74:30)

Definitions is missing on the exported module.
So I wrote my own transform rules based on the graphql-tag/loader

const gql = require('graphql-tag');

// Takes `source` (the source GraphQL query string)
// and `doc` (the parsed GraphQL document) and tacks on
// the imported definitions.
function expandImports(source, doc) {
  const lines = source.split('\n');
  let outputCode = "";

  lines.some((line) => {
    if (line[0] === '#' && line.slice(1).split(' ')[0] === 'import') {
      const importFile = line.slice(1).split(' ')[1];
      const parseDocument = `require(${importFile})`;
      const appendDef = `doc.definitions = doc.definitions.concat(${parseDocument}.definitions);`;
      outputCode += appendDef + "\n";
    }
    return (line.length !== 0 && line[0] !== '#');
  });

  return outputCode;
}

module.exports = {
  process(src, _filename, _config, _options) {
    const doc = gql`${src}`;
    const outputCode = `var doc = ${JSON.stringify(doc)};`;
    const importOutputCode = expandImports(src, doc);

    return outputCode + "\n" + importOutputCode + "\n" + `module.exports = doc;`;
  },
};

And the jest config:

      "^.+\\.(gql|graphql)$": "<rootDir>/config/jest/graphqlTransform.js"

This work fine now

@xavxyz

This comment has been minimized.

Copy link
Contributor

xavxyz commented May 17, 2017

This got fixed with v2.1.0 normally 👍 I believe we can close this issue 🙃

@turadg turadg closed this Oct 16, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.