Skip to content

Add typescript definitions #1

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

Closed
rricard opened this issue Jun 29, 2016 · 15 comments
Closed

Add typescript definitions #1

rricard opened this issue Jun 29, 2016 · 15 comments

Comments

@rricard
Copy link

rricard commented Jun 29, 2016

For now, in some use cases, TS (in atom) warns: Argument of type 'TemplateStringsArray' is not assignable to parameter of type 'string'. for using gql like this:

gql` ... `
@rricard
Copy link
Author

rricard commented Jun 29, 2016

Found a fix to that: your typescript is too old. if you are using atom, please change the path to the typescriptServices in your config
settings______users_rricard_developer_github.com._apollostack_apollo-client

@rricard rricard closed this as completed Jun 29, 2016
@philcockfield
Copy link

@rricard - is this working for you? I updated the typescriptServices.js file as you showed, to the version of TypeScript I'm using, but I'm still getting the build error:

.../graphql.spec.tsx(26,22): error TS2345: Argument of 
type 'TemplateStringsArray' is not assignable to parameter of 
type 'string'.

image

Where are you getting your typescriptServices.js file from?

@stubailo stubailo reopened this Jul 8, 2016
@rricard
Copy link
Author

rricard commented Jul 8, 2016

I installed the latest tsc from npm using npm i -g typescript.

@philcockfield
Copy link

@stubailo FYI here's the wrapper hack I wrote to get this working in TS:

import gql from 'graphql-tag';
import { Document } from 'graphql';



/**
 * Processes the given template string parts.
 */
function formatTemplate(literals: Array<string>, ...placeholders: Array<any>): any {
  const parts = [];
  literals.forEach((item, i) => {
    parts.push(item);
    if (placeholders[i]) {
      parts.push(placeholders[i]);
    }
  });
  return [parts.join('')];
}



/**
 * Process the given template-string into a GraphQL query.
 *  NOTE:
 *    This is a layer of indirection to `graphql-tag` because
 *    TypeScript does not play nicely with the module at the moment.
 */
export default (literals: Array<string>, ...placeholders: Array<string>): Document => {
  const query = formatTemplate(literals, ...placeholders);
  return (<any> gql)(query);
};

@philcockfield
Copy link

philcockfield commented Jul 8, 2016

The type definition file that produces is:

import { Document } from 'graphql';
declare var _default: (literals: string[], ...placeholders: string[]) => Document;
export default _default;

@stubailo
Copy link
Contributor

stubailo commented Jul 9, 2016

Hmm, how do dependencies in TypeScript definition files work? Since this package doesn't actually have a dependency on GraphQL, is it going to be weird?

@philcockfield
Copy link

I don't thinks so. This is what you already have in your index.d.ts
https://github.com./apollostack/graphql-tag/blob/master/index.d.ts

import { Document } from 'graphql';
export default function gql(str: string): Document;

Where/how did you generate that file?

My understanding (as it stands) is that you don't need this package to reference graphql, however when someone installs this package within a TS project, they will need to add typings for graphql:

typings install graphql --save

@stubailo, why not write this module in TS directly, like Apollo-Client

@stubailo
Copy link
Contributor

stubailo commented Jul 9, 2016

Where/how did you generate that file?

I wrote it by hand!

why not write this module in TS directly

Honestly it's too much work to set up the build process for a module this simple - it's like 10 lines of code.

@philcockfield
Copy link

Fair enough.

@stubailo
Copy link
Contributor

Anyone have the time to write a better definition?

@philcockfield
Copy link

philcockfield commented Jul 14, 2016

@stubailo - I suspect this would do the trick:

index.d.ts

import { Document } from 'graphql';
export default function gql(literals: Array<string>, ...placeholders: Array<string>): Document;

@stubailo
Copy link
Contributor

OK I'll try this one and republish soon!

@mquandalle
Copy link

mquandalle commented Jul 18, 2016

@stubailo I pushed the fix proposed by @philcockfield, which works as expected and fixes the OP bug. Please publish on NPM if that’s good for you.

@stubailo
Copy link
Contributor

Publishing now!

@stubailo
Copy link
Contributor

Done - published as 0.1.10!

mquandalle pushed a commit to apollographql/apollo-client that referenced this issue Jul 18, 2016
Updating graphql-tag was also required because it’s incorect types definition
was raising errors with TS 2.0, see apollographql/graphql-tag#1.

I haven’t opt-in for the `strictNullChecks` option yet, for that’s certainly
something to do to get the full benefit of this new TS release.
mquandalle pushed a commit to apollographql/apollo-client that referenced this issue Jul 18, 2016
Updating graphql-tag was also required because it’s incorect types definition
was raising errors with TS 2.0, see apollographql/graphql-tag#1.

I haven’t opt-in for the `strictNullChecks` option yet, for that’s certainly
something to do to get the full benefit of this new TS release.
mquandalle pushed a commit to apollographql/apollo-client that referenced this issue Jul 19, 2016
Updating graphql-tag was also required because its incorect types definition
was raising errors with TS 2.0, see apollographql/graphql-tag#1.

I haven’t opt-in for the `strictNullChecks` option yet, for that’s certainly
something to do to get the full benefit of this new TS release.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants