Skip to content

Commit

Permalink
feat(shared): 添加graphql模块
Browse files Browse the repository at this point in the history
  • Loading branch information
stbui committed Sep 2, 2018
1 parent 3f1d8e6 commit a8cf2a7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
24 changes: 24 additions & 0 deletions docs/graphql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Graphql

```
ng add apollo-angular
npm i --save apollo-client apollo-cache-inmemory apollo-angular-link-http apollo-angular graphql-tag graphql
```

```
{
...
"lib": [
"es2017",
"dom",
"esnext.asynciterable"
]
}
}
```

## 参考资源

- https://github.com/apollographql/apollo-angular
- https://launchpad.graphql.com/
42 changes: 42 additions & 0 deletions src/app/shared/graphql.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NgModule } from '@angular/core';
import { HttpClientModule, HttpHeaders } from '@angular/common/http';
import { ApolloModule, APOLLO_OPTIONS } from 'apollo-angular';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';

import { setContext } from 'apollo-link-context';
import { ApolloLink } from 'apollo-link';

const uri = 'https://api.github.com/graphql';

export function createApollo(httpLink: HttpLink) {
const basic = setContext((op, ctx) => ({
headers: new HttpHeaders().set('Accept', 'charset=uf-8')
}));

const auth = setContext((operation, ctx) => ({
headers: ctx.headers.append(
'Authorization',
`Bearer 92ad2f89a0eaa8693abdf05d5d4889c70873491b`
)
}));

const link = ApolloLink.from([basic, auth, httpLink.create({ uri })]);

return {
link,
cache: new InMemoryCache()
};
}

@NgModule({
exports: [HttpClientModule, ApolloModule, HttpLinkModule],
providers: [
{
provide: APOLLO_OPTIONS,
useFactory: createApollo,
deps: [HttpLink]
}
]
})
export class GraphQLModule {}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],
"lib": [
"es2017",
"dom"
"dom",
"esnext.asynciterable"
]
}
}

0 comments on commit a8cf2a7

Please sign in to comment.