Skip to content

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.

4 participants