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

Provide a way to dump schema to stdout. #512

Merged
merged 1 commit into from Sep 9, 2018

Conversation

kalekseev
Copy link
Contributor

It can be useful if you need schema.graphql instead of json eg:
graphql_schema --schema project.schema --out=- | node bin/json-to-gql.js --src - --out project/schema.graphql

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.08%) to 94.508% when pulling 85527e1 on kalekseev:schema-to-stdout into f4bbae2 on graphql-python:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.08%) to 94.508% when pulling 85527e1 on kalekseev:schema-to-stdout into f4bbae2 on graphql-python:master.

@syrusakbary syrusakbary merged commit f76f38e into graphql-python:master Sep 9, 2018
@andykenward
Copy link

@kalekseev where does the code in json-to-gql.js come from? Is it a custom script?

I need to convert the output to .graphql

@kalekseev
Copy link
Contributor Author

@andykenward

django-admin graphql_schema --out=/tmp/schema.json
node json-to-gql.js --src /tmp/schema.json --out schema.graphql

json-to-gql.js

const { buildClientSchema, printSchema } = require('graphql');
const fs = require('fs');

let src = null;
let out = null;

process.argv.forEach(function(val, index, array) {
  if (val === 'src') {
    src = array[index + 1];
  }
  switch (val) {
    case '--src':
      src = array[index + 1];
      break;
    case '--out':
      out = array[index + 1];
      break;
  }
});

function writeSchema(fileContent, output) {
  if (!fileContent) {
    console.error('no schema found');
    return;
  }
  const { data } = JSON.parse(fileContent);
  const clientSchema = buildClientSchema(data);
  const graphqlSchemaString = printSchema(clientSchema);
  fs.writeFileSync(output, graphqlSchemaString);
}

if (!src) {
  console.error('must provide src path');
} else if (!out) {
  console.error('must provide out path');
} else {
  if (src === '-') {
    let data = '';
    process.stdin
      .on('data', function(d) {
        data += d;
      })
      .on('end', function() {
        writeSchema(data, out);
      })
      .setEncoding('utf8');
  } else {
    const fileContent = fs.readFileSync(src, 'utf-8');
    writeSchema(fileContent, out);
  }
}

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

Successfully merging this pull request may close these issues.

None yet

4 participants