lewisf / extract-gql

2 stars 1 forks source link

Audit public interface to make sure it makes sense. #1

Open lewisf opened 7 years ago

lewisf commented 7 years ago

This has been pulled from persistgraphql (https://github.com/apollographql/persistgraphql/issues/19) to be used as a standalone utility / tool. As such, we should audit the public interface to make sure it makes sense and make sure to be meaningful with what we expose.

Poincare commented 7 years ago

@lewisf

Couple of thoughts regarding the public interface and some minor implementation details.

It seems to me that there are a few benefits to handling AST-level stuff in this package, such as:

  1. It will make it easy to build tools like the in this issue
  2. It will make the persistgraphql tool a very small layer on top of this package.

Thoughts?

lewisf commented 7 years ago

@Poincare thanks for the comments, they definitely help a lot.

Maybe it makes sense to return an AST to facilitate the building of some tools, but also allow just returning raw strings in cases where:

I'm thinking about apollo-codegen where right now it lacks the capability to extract from javascript source files and extract-gql could enable that. Eventually it may also make sense to move apollo-codegen to just use the AST's from extract-gql too.

I think allowing for both raw strings and ASTs doesn't muddy up the responsibilities of this package that much and allows it to provide more utility.

Poincare commented 7 years ago

Exposing both sets of methods (i.e. strings and AST) sounds great to me if it seems like just the strings would be useful as well.

lewisf commented 7 years ago

@Poincare cool. what might make sense in terms of an interface here?

new ExtractGQL(parseIntoAST: true)

or

const extractor = new ExtractGQL();
extractor.fromJS().toString()
extractor.fromJS().toAST()

or

import {
  ExtractGQLString,
  ExtractASTString,
} from 'extract-gql';

new ExtractGQLString()
new ExtractGQLAST()

I'll have to think about it a bit, but just laying out some options for discussion, thoughts?

Poincare commented 7 years ago
const extractor = new ExtractGQL();
extractor.fromJS().toString()
extractor.fromJS().toAST()

looks good to me as a concept. However, what if a particular JS file contains multiple queries? Would this just put them into a single string? I think it would probably be better to place them in a list.

lewisf commented 7 years ago

Good point, probably toStrings(), returning Array<string> makes more sense in that case.

Poincare commented 7 years ago

Right. I think toAST() should also return a list of ASTs rather than a single AST since we'd want to be able to differentiate between queries mentioned in different parts of the code.