Created
September 25, 2018 14:04
-
-
Save shsunmoonlee/739a5ea4a02a2eadac68898127511da0 to your computer and use it in GitHub Desktop.
graphql question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // client | |
| const GET_RECORDINGS = gql`{ | |
| recordings { | |
| id | |
| title | |
| input | |
| } | |
| }` | |
| const ADD_RECORDINGS = gql` | |
| mutation($title: String!, $input: AudioInput! ) { | |
| addRecording(title: $title, input: $input) { | |
| title | |
| input | |
| } | |
| } | |
| `; | |
| <Mutation mutation={ADD_RECORDINGS} variables={{ title: 'testing mutation recordign title1', input: [{midiNumber: 1, currentTime: 1}] }}> | |
| {(addRecording) => ( | |
| <button | |
| className={'RepositoryItem-title-action'} | |
| onClick={addRecording} | |
| > | |
| Record | |
| </button> | |
| )} | |
| </Mutation> | |
| <Query query={GET_RECORDINGS}> | |
| {({ data, loading }) => { | |
| // const { viewer } = data; | |
| console.log("===GET_RECORDINGS QUERY RESULT data", data) | |
| // if (loading || !viewer) { | |
| // return <div>Loading ...</div>; | |
| // } | |
| if (loading) { | |
| return <div>Loading ...</div>; | |
| } | |
| return ( | |
| <div> | |
| get_recordings_result query | |
| </div> | |
| ); | |
| }} | |
| </Query> | |
| // server | |
| const { ApolloServer, gql } = require('apollo-server'); | |
| const recordings = [ | |
| { | |
| id: 1, | |
| title: 'Recording 1 graphql example', | |
| input: [ {midiNumber: 51, currentTime: 1}, {midiNumber: 52, currentTime: 2}, {midiNumber: 53, currentTime: 3}], | |
| } | |
| ]; | |
| const typeDefs = gql` | |
| type Recording { | |
| id: ID! | |
| title: String | |
| input: [AudioOutput] | |
| } | |
| input AudioOutput { | |
| midiNumber: Float | |
| currentTime: Float | |
| } | |
| input AudioInput { | |
| midiNumber: Float | |
| currentTime: Float | |
| } | |
| type Query { | |
| recordings: [Recording] | |
| } | |
| type Mutation { | |
| addRecording(title: String, input: [AudioInput]): Recording | |
| } | |
| ` | |
| const resolvers = { | |
| Query: { | |
| recordings: () => recordings, | |
| }, | |
| Mutation: { | |
| addRecording: (_, { title, input }) => { | |
| const newRecording = { | |
| id: recordings.length + 1, | |
| title, | |
| input, | |
| }; | |
| recordings.push(newRecording); | |
| return newRecording; | |
| } | |
| } | |
| } | |
| const server = new ApolloServer({ typeDefs, resolvers }); | |
| server.listen().then(({ url }) => { | |
| console.log(`Apollo server running: ${url}`); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment